1
我有一個簡單has_and_belongs_to_many關係在Mongoid設置像這樣:mongoid before_destroy回調
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模型和破壞的作者/郵政的關係,我該怎麼辦的before_destroy或一些其他的回調作者更新帖子計數?
我需要它的工作更新時,郵政,不刪除 - 是不是也有類似的方法,我可以用這個? – koosa
我的歉意我讀錯了這個問題。 Mongoid也爲關聯更改提供了回調,我更新了答案。 –