ruby 34 lines · 6 steps

How counter caches work in Rails

A counter_cache column keeps a running tally of associated records so you never pay for a COUNT query.

Explained by highlit
1class CreateCommentsWithCounterCache < ActiveRecord::Migration[7.1]
2 def change
3 create_table :posts do |t|
4 t.string :title, null: false
5 t.integer :comments_count, null: false, default: 0
6 t.timestamps
7 end
8 
9 create_table :comments do |t|
10 t.references :post, null: false, foreign_key: true
11 t.text :body, null: false
12 t.timestamps
13 end
14 end
15end
16 
17class Post < ApplicationRecord
18 has_many :comments, dependent: :destroy
19end
20 
21class Comment < ApplicationRecord
22 belongs_to :post, counter_cache: true
23end
24 
25class BackfillPostCommentsCount < ActiveRecord::Migration[7.1]
26 def up
27 Post.find_each do |post|
28 Post.reset_counters(post.id, :comments)
29 end
30 end
31 
32 def down
33 end
34end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A counter_cache trades a little write-time bookkeeping for fast, query-free association counts.
  2. 2The cache column must be non-null with a default of 0 so increments and decrements stay consistent.
  3. 3reset_counters recomputes tallies from scratch, which is essential when backfilling an existing table.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How counter caches work in Rails — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code