Counter Cache Reset for Counters


When you want to implement counter cache, how to handle the counters for the old data? Generally you will add a migration which runs a script to update all the counts, unfortunately you wil get an error, because the column is readonly, you can't update this column in a normal way.

Model

    class Post < ActiveRecord::Base
      belongs_to :category, :counter_cache => :resources_count
    end

Incorrect way

    class InitializeResourcesCountForCategories < ActiveRecord::Migration
      def up
        Category.find_each do |cat|
          cat.update_attribute(:resources_count => cat.posts.count)
        end
      end
    end

You should be using User.reset_counters to do this

    class InitializeResourcesCountForCategories < ActiveRecord::Migration
      def up
        Category.find_each do |cat|
          Category.reset_counters(cat.id, :posts)
        end
      end
    end

Posted at 01 Jan 2012

blog comments powered by Disqus