-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
Labels:
The following code crashes on Mongoid 2.4.2, and from the code it looks like it will in 3.0 as well.
class A include Mongoid::Document has_many :bs, :inverse_of => :a end class B include Mongoid::Document belongs_to :a, :inverse_of => :bs belongs_to :c, :inverse_of => :b end class C include Mongoid::Document has_one :b, :inverse_of => :c end a = A.new b = B.new c = C.new b.c = c b.a = a b.c = c
Description of why this crashes from my gist on this:
> What happens is that in response to b.a = a, in Mongoid::Relations::Bindings::Referenced::In#bind on line 32, b.metadata is set to the Metadata belonging to this Many relation. This causes b.referenced_many? to return true.
>
> When b.c = c is called then, in Mongoid::Relations::Bindings::Referenced::In#unbind on line 62, b.referenced_many? is checked and found to return true, even though the :c relation is of the One kind. This in turn causes #delete(<Document>) to be called on the single Document, as if it were an Array. (Which it would have been in case of a Many relation.) This #delete call is interpreted as an attempt to delete the model, but that method expects a Hash of options, not a Document. When in the end #merge! is called on this supposed Hash, the scripts crashes as it was actually called on a Document.