我在 Mongoid 中设置了一个简单的 has_and_belongs_to_many 关系,如下所示:
class Post
...
has_and_belongs_to_many :authors
scope :live, lambda{ published.where(:published_at.lt => Time.now) }
end
class Author
has_and_belongs_to_many :posts
before_save :count_posts
def count_posts
self.post_count = posts.live.length
end
end
当我更新 Post 模型并销毁 Author / Post 关系时,如何对作者执行 before_destroy 或其他回调来更新帖子计数?