Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twin::Collection can't remove members backed by a DB table with not-null constraint #41

Open
niels opened this issue Apr 22, 2016 · 2 comments

Comments

@niels
Copy link
Contributor

niels commented Apr 22, 2016

Suppose I have a Twin A that contains a collection of Twins B. Twin B is backed by a database table. Collection membership is indicated by a twin_a_id column on the Twin B database table. That column has a not-null constraint as no Twin B should ever exist that isn't associated with a Twin A.

Given the above, I can't remove a Twin B from the collection. Twin::Collection always wants to call Twin::Collection#delete, even when actually using #destroy. #delete will try to persist to the database by nullifying twin_a_id, thus violating the not-null constraint, thus raising an exception.

Perhaps #destroy should just destroy the associated Twin (as one might naively expect the name to imply)? Users who actually want to delete, then destroy, would then have to do so manually by calling both methods. Of course this would be a backwards-incompatible change..

@niels niels changed the title Twin::Collection doesn't provide a "direct delete" Twin::Collection can't remove members backed by a DB table with not-null constraint Apr 22, 2016
@niels
Copy link
Contributor Author

niels commented Apr 22, 2016

My workaround:

module Disposable
  class Twin
    class Collection
      def direct_destroy(twin)
        delete_at(index(twin))
        twin.model.destroy
      end
    end
  end
end

@apotonick
Copy link
Owner

Oh, I didn't even know there's a different behavior implicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants