counter_cache gotcha in activerecord!

If you are using counter_cache, you must watch out for this one. I am not sure if this has been fixed in the recent version, but the version that I am using (1.15.3) has the issue

If you are using counter_cache (such as in acts_as_commentable as described here), you must make sure that after updating the counter (in after_create call in this case), you must not save your commentable object (in this case @song) without reloading it, otherwise the counter will be reset back to the value before the increment.

Took me a while to figure this out and I googled after that and found that other people have faced this issue too. You can read more about the error here

Popularity: 11% [?]

find this blog feedworthy. Click on the number to subscribe!

Related Posts

Move to Rails 2.0.2

Rails : Accessing session data in models

Active record and Mongrel Upload Progress Plugin

Comments

Thank you for sharing!

Leave a comment

(required)

(required)