Rails nested attributes children callbacks aren't fired Rails nested attributes children callbacks aren't fired ruby-on-rails ruby-on-rails

Rails nested attributes children callbacks aren't fired


I had the same issue. before_save callback is not called when the model is not changed.

You're updating line_items, not the budget, so rails thinks that it is not updated and doesn't call save for it.

You need to change before_save to after_validation so it will be called even if model has no changed attributes. And when in this callback you change some attributes, rails will see that your model had changed and will call save.


Old question, I know, but it still comes up first in search. I think this article has a solution:

Rails, nested attributes and before_save callbacks

If I'm understanding that article correctly, the problem (as @AntonDieterle explains in his answer) is that the child callback isn't triggered because the parent isn't "dirty." This arcticle's solution is to "force" it to be dirty by calling attr_name_will_change! on a parent attribute that, in fact, does not change. See [Active Model Dirty] in the Rails API2.

Anton's solution of using after_validation instead of before_save sounds simpler, but I wanted to put this out there an an alternative.