counter_casheをリセットする

counter_casheを使ってPostに紐付くCommentの数をDBカラムに保存するように修正した。

PostにCommentが追加/削除されたらCommentの数が変更される。

class Comment < ActiveRecord::Base
  belongs_to :post, :counter_cache => :comments_count
end

上記の変更を反映させるために、
すべてのPostについてCommentの数を再計算してカラムに保存する以下のスクリプトが必要になった。

Post.find_each do |post|
  Post.reset_counters post.id, :comments
end

参考:

stackoverflow.com