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



has_many :posts

post.rb - add counter_cache: true to recalculate posts_count field in user table

belongs_to :user, counter_cache: true


rails g migration add_posts_count_to_users posts_count:integer


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) }