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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---