Formal way to reset counters

Has there ever been discussion of adding a method to recalculate
counter cache values?

This seems like it would be useful both for adding counter caches to
existing tables, and also for fixing things up when direct SQL
manipulations are used (performance reasons in my case).

There are actually two parts to the problem. The simple part is that
the update_counters only supports incremental change, so doing a full
reset would require some awkward arithmetic.

The more complex part is using the association reflections to
calculate the actual value automatically. Doing so would require the
participation of both the belongs_to reflection *and* the has_many
reflection, which I can see being a bit smelly given the fact that the
counter cache currently works without the need for has_many to be
declared at all. However I think it's utility might outweigh the

What do you guys think?

+1 for a public API

I’ve been using this :frowning:


Brilliant find Mike, good work hardbap, and thanks for applying
Jeremy, but I don't actually like this API because it doesn't let you
update a single row. If you have lots of rows this will be
unacceptably slow, as in slower than the amount of time it would take
to manually write and execute an SQL query to do the reset.

I'm going to reopen with an additional patch with how I think it
should be done.

Please do!


It's up. Doesn't look like I can change the ticket status though.

pardon the whitespace noise in the ticket

also FYI, I ran tests against mysql,sqlite and postgres and had 1
mysql failure and 2 postgres failures independent of this.