我有一个模型Topic和Post。Topic has_many:posts。
在Topic模型中,我还有accepts_nested_attributes_for:posts,
当使用一些参数更新Topic时,似乎Post模型的观察器和回调都不会被触发。这是一个错误还是一个特性?
我有一个模型Topic和Post。Topic has_many:posts。
在Topic模型中,我还有accepts_nested_attributes_for:posts,
当使用一些参数更新Topic时,似乎Post模型的观察器和回调都不会被触发。这是一个错误还是一个特性?
我不确定你所说的"当更新帖子时使用一些参数来更新主题"是什么意思,但是只有在更新帖子对象时,Post
回调才会运行。
也就是说,以下情况会触发回调:
@post.update_attributes(:topics_attributes => [...])
...但这不会:
@topic.update_attributes(:post_id => 123, ...)
Post
after_update
回调,可以执行以下操作:# topic.rb
after_update :touch_post
private
def touch_post
post.touch
end
观察者代码的行为类似于回调函数。