Basically, if I am updating a belongs_to associated object and I am
updating the value of the foreign key for the association, no attempt is
made by rails to modify the counter_cache.
AR#save by default saves all fields. I can’t remember exactly what
happens since I don’t use counter_cache anymore. Have you checked the
log to see if it actually is getting updated but that the update is lost
when you save the save the parent record? This was how I first noticed
the problem. Also, I’m not sure if the counter_cache code triggers
unless you do it thru the parent.
Ie. if you have class Forum which has many Class Article, and you have
an article_count in your Forum class, you may need to do
forum_instance.articles << Article.new to get that to trigger. Just
I only use the caches in a non-critical area of the admin section of my
site. It is enough to update them periodically. The only thing I have
noticed this does affect is pagination.
If the counter cache is a lower value than the actual number of
associated records, pagination will only give me as many pages as the
cache value would indicate.
I will steer clear of dependency on counter caches but they are
necessary to cut down queries to the db on result list pages. Perhaps
with a little indexing, even that won’t be a problem.
Or you can patch AR to not include *_count fields when doing AR#save,
and use AR#increment_counter for the *_count fields.