apotonick / disposable

Decorators on top of your ORM layer.
MIT License
171 stars 39 forks source link

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

Open niels opened 8 years ago

niels commented 8 years ago

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 commented 8 years ago

My workaround:

module Disposable
  class Twin
    class Collection
      def direct_destroy(twin)
        delete_at(index(twin))
        twin.model.destroy
      end
    end
  end
end
apotonick commented 8 years ago

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