[ACCEPTED]-Rails nested attributes children callbacks aren't fired-nested-attributes

Accepted answer
Score: 39

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

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

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

Score: 10

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

Rails, nested attributes and before_save callbacks

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

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

More Related questions