Rails counter cache not updating

posted by | Leave a comment

I migrate tables where some inital row values in the db are generated using Model.create in the migration class, which cannot be done with counter caching.Instead I just made a rake file with the same Model.create calls and that seems to work, if I just generate the values in a correct order. Ryan, do you have any experience or advice on working with counter_caches that also have conditions on them?This is because the counter caches are incremented or decremented when you create or delete an instance of the model. Counting associations represents a performance improvement by avoiding eager loading and N 1 queries for listings -- e.g.

rails counter cache not updating-44rails counter cache not updating-83

For example, a comments_count column in a Post class that has many instances of Comment will cache the number of existent comments for each post.

After trying for a while, I've come to the conclusion that it is best not to use counter cache for these types of associations.

It is terrible for performance because it generates several SQL queries that could be cut down to just 4 or less queries. Let's say we have posts with tags through taggings.

I think it's not possible any more in rails edge (2.0) to fill the count-colums afterwards, because (from the edge api): "Note: Specifying a counter_cache will add it to that model’s list of readonly attributes using attr_readonly." Some other idea for this?

Beate, I recently posted about this very issue on my blog. Check it out: Thanks for the information about how to add a counter cache to an existing db However I found that solution a little bit to complex for me.

Leave a Reply

world s best dating sites