Rails counter cache associations
Rails provides an ability to cache count of associated table records. This gives an edge that avoids querying to get number of records of associated table.
This is useful for having count of has many association records. It is available on belongs_to options on model.
Let’s take an example.
We have a users
table.
User
model given below.
And a user
has many posts
.
Post
model given below
Without counter_cache in Rails
Let’s say we want to list users with a count of posts by a user. First, we will fetch posts to be displayed in controller action.
Then, we will display them in view erb
as given below.
Now, this works well to render count of posts per user.
But, this will query posts
for every user object being looped over.
With counter_cache
1. Add a new column
Add a field on users to keep the track of number of posts by the user. We will need to add the field with following naming convention.
In the case discussed above, we will use column name given below.
2. Mention counter_cache on association
We will need to add counter_cache
option on post belongs_to user association.
That’s all.
This will make sure that posts_count
on users
has updated value
of
number of posts for the user.
3. Access cached counter value
We don’t need to change the way we access number of records of associated table.
This will work and use value from newly added column.
4. Populate count value of existing records
To populate values for the newly added column, Rails provides a method reset_counters.
We can populate counter with a migration with looping over all users
as given below.
Reference
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox