You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We noticed that with embedded documents the before_create callback is called twice - once for the top level document and once for the embedded document. This results in two issues:
a) the sequence is no longer sequential
b) (the bigger issue) the object id of the in memory object is different from the object id of the persisted object. Meaning if you reference that object's id without reloading it you will be storing invalid references in other objects.
Example:
classObjincludeMongoid::Documentauto_increment:_id,:collection=>:obj_id_sequence,:seed=>1embeds_one:obj_configafter_initializedo |c|
c.create_obj_configunlessc.obj_configendendclassObjConfigincludeMongoid::Documentembedded_in:objendc1=Obj.create!()# before_create called twiceputsc1.id# outputs 3
Is there a way to handle this aside from not calling before_create for the embedded objects?
Thanks.
The text was updated successfully, but these errors were encountered:
We noticed that with embedded documents the before_create callback is called twice - once for the top level document and once for the embedded document. This results in two issues:
a) the sequence is no longer sequential
b) (the bigger issue) the object id of the in memory object is different from the object id of the persisted object. Meaning if you reference that object's id without reloading it you will be storing invalid references in other objects.
Example:
Is there a way to handle this aside from not calling before_create for the embedded objects?
Thanks.
The text was updated successfully, but these errors were encountered: