Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cascade_callbacks not working for embedded doc in mongoid

I'm using Mongoid, and I have one class (Order) that "embeds_many" of another class (LineItem). Specifically, in Order, I have:

embeds_many :line_items, cascade_callbacks: true

And in LineItem, I have:

embedded_in :order

Everything works as it should except for the cascading_callbacks. When I make a change to an Order object and call "save", I would expect the "after_save" callbacks on all of the embedded LineItem objects to be called. This is not happening. I thought that was the point of cascading_callbacks.

Am I incorrect? Is something else required to make the save-related callbacks fire for each embedded document when the parent is saved and cascade_callbacks is set to true in the relationship?

like image 662
user2571294 Avatar asked Sep 01 '25 05:09

user2571294


1 Answers

I think, when you specify cascade callbacks: true in the parent document, the callbacks of the embedded documents are fired only when you make any change in the corresponding embedded document.

To check, change something in the embedded document and save the parent document. In this case I think the callbacks will be fired

like image 166
aash Avatar answered Sep 02 '25 19:09

aash