Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 31 lines (28 sloc) 1.059 kb
layout title categories
reset_counters in rails

I thought reset_counters method is to reset a counter_cache column to be 0, but it is not. After trying several times, I finally realize that reset_counters is to update the value of counter_cache column to the exact count of associations. The usecase of reset_counters is when you add the counter_cache in migration and update the counter_cache value, like

{% highlight ruby %} def self.up add_column :posts, :comments_count Post.all.each do |post| Post.reset_counters(, :comments) end end {% endhighlight %}

it will add comments_count column to posts table, and calculate the comments count for each post, and set it to posts' comments_count column.

I didn't find a method to reset the counter_cache column to be 0, why? Because counter_cache is used to cache the association count, it will be incremented and decremeneted automatically, you should never reset it 0. If you find you need to reset counter_cache to 0, that means it's a wrong usage of counter_cache.

Something went wrong with that request. Please try again.