counter_cache - count how many children a records has
Often you want to count how many child records a record has (user.posts.count
, user.comments.count
).
Storing this data in a the database is more efficient (like user.posts_count
, user.comments_count
) than recalculating it each time.
counter_cache
gives us a way to recalculate the database field containing count of child records whenever a child record is created/deleted
HOWTO #
user.rb
has_many :posts
post.rb - add counter_cache: true
to recalculate posts_count
field in user table
belongs_to :user, counter_cache: true
console:
rails g migration add_posts_count_to_users posts_count:integer
migration:
add_column :users, :posts_count, :integer, default: 0, null: false
rails c - recalculate posts_count for all existing posts
and users
User.find_each { |u| User.reset_counters(u.id, :posts) }
Did you like this article? Did it save you some time?