我也有一定的要求看柜台的变化。在挖掘rails源代码之后,通过直接的SQL更新来改变counter_column。换句话说,它不会触发任何回调(在你的情况下,当Post更新时它不会触发Author模型中的任何回调)。
from rails源代码,counter_column也被after_update回调改变了。
我的做法是给轨道的一路上涨,由我更新counter_column:
class Post
belongs_to :author
after_update :update_author_posts_counter
def update_author_posts_counter
# need to update for both previous author and new author
# find_by will not raise exception if there isn't any record
author_was = Author.find_by(id: author_id_was)
if author_was
author_was.update_posts_count!
end
if author
author.update_posts_count!
end
end
end
class Author
has_many :posts
after_update :expires_cache, if: :posts_count_changed?
def expires_cache
# do whatever you want
end
def update_posts_count!
update(posts_count: posts.count)
end
end
什么是在具有反缓存在所有那么点? – 2012-01-25 12:55:26