Skip to content

Commit ebed70d

Browse files
committed
denormalize_destroy_to_* trigger before destroy
The to_alize destroy callbacks used to be a after_destroy callback which failed when relations specified a :nullify dependency - since mongoid would nullify all relations before the destroy and the after_destroy callback would find no children after the destroy op to sync. Changed the callback to be triggered before the destroy op, except in the case of a :restrict dependency where a before_destroy callback would pre-maturely sync the child objects even though the destroy op may never happen
1 parent a4b297a commit ebed70d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/mongoid/alize/to_callback.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ def set_callback
132132

133133
def set_destroy_callback
134134
unless callback_attached?("destroy", aliased_destroy_callback_name)
135-
klass.set_callback(:destroy, :after, aliased_destroy_callback_name)
135+
if self.metadata.dependent == :restrict
136+
klass.set_callback(:destroy, :after, aliased_destroy_callback_name)
137+
else
138+
klass.set_callback(:destroy, :before, aliased_destroy_callback_name)
139+
end
136140
end
137141
end
138142

0 commit comments

Comments
 (0)