Counter-cache AFTER database already populated

I need to use the counter-cache functionality in my model.
I defined as stated the counter-cache => true; on the belongs_to side
and a xxx_count column on the has_many side.
In my database this column is now 0.
How can I ‘repair’ the count… as obviously it should be equal to the
number of items already stored on the many side… ?
should I write a procedure ? a specific action in my app ?
btw : the table on the one side is 38’000 items… which exclude doing
it by-hand !

kad

Kad,

If you want to initialize the counter cache after having modified the
db schema, simply run set the value in the migration like:

class AddArticlesCounterCache < ActiveRecord::Migration
def self.up
add_column :blogs, :articles_count, :integer

for blog in Blog.find(:all) do
  blog.articles_count = blog.articles.size
  blog.save
end

end

def self.down
remove_column :blogs, :articles_count
end
end

Alain