Observer and callback do not start for accepts_nested_attributes_for

I have Subject and Mail models. Has_many topic: posts.

The theme model also has accepts_nested_attributes_for: posts,

It seems that neither the observer nor the callback are triggered for the Post model when updating the theme with some options for Post. Is this a bug or a function?

+4
source share
2 answers

Not sure what you mean by "when updating the theme with some options for Post," but Post callbacks will only be executed when the post object is updated.

those. this will cause callbacks:

 @post.update_attributes(:topics_attributes => [...]) 

... but it will not:

 @topic.update_attributes(:post_id => 123, ...) 

If you want to call the Post after_update when updating the theme, you can do this:

 # topic.rb after_update :touch_post private def touch_post post.touch end 

The observer code will behave similarly to callbacks.

0
source

When you run the update_attributes method, on the nested resource or not, the resource is saved only if it has been changed , and if it passes the check: If not, the resource is not saved and after_update does not start.

0
source

Source: https://habr.com/ru/post/1387713/


All Articles