Hi all,
I'm getting some interesting results in a migration (Rails 2.3.5)
class Comment < ActiveRecord::Base
..
belongs_to :item, :counter_cache => "comment_counter_cache"
..
end
Migration:
change_column :items, :comment_counter_cache, :integer, :default => 0
Item.find(:all).each do |item|
item.update_attribute( :comment_counter_cache, item.comments.size )
new_size = item.comments.all.size
item.update_attribute( :total_comment_counter_cache, new_size )
item = Item.find(item.id)
puts "item '#{item.name}': " +
"new size (value actually assigned to comment counter cache): '#{new_size}'" +
"all comments size = '#{item.comments.all.size}', " +
"num comments = '#{item.num_comments}', " +
"comment_counter_cache = '#{item.comment_counter_cache}'"
end
...
def num_comments
self.comment_counter_cache
end
Mostly the numbers are the same. Except sometimes I get this kind of thing:
item 'Item 1': new size (value actually assigned to comment counter cache): '237'all comments size = '237', num comments = '235', comment_counter_cache = '235'
Any thoughts on how this is possible? What is interfering with the comment_counter_cache to have a value 2 lower than the value I just gave it? Bizarre. Clearly I'm screwing something up and wanted to know if anyone had any pointers as to where it might all be going horribly wrong.