如何检测关联对象的数量是否发生变化?

时间:2014-11-05 10:18:33

标签: ruby-on-rails activerecord

class Post < ActiveRecord::Base
  has_many :comments

  after_save :do_something, if: -> (post) { [ something like post.comments.count.changed? ] }
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

如果评论数量发生变化,如何执行do_something?

如果我通过示例检测到3个以上的更改,我想执行do_something:添加2个注释,删除2个。

0 个答案:

没有答案