Sometimes calculating the count of a collection on every page render, even in SQL, is not the best option.
Imagine that you need to render a post, with its count of likes and comments, but the post has thousands of likes and comments. That would be slow.
One solution is to save the count of those comments and likes in the record, and when you need to render the post, instead of counting the elements again, just display the count that you already saved.
Keeping that count in sync is not easy, but rails makes it easy for us with a feature called “counter cache”.
Here I will show you how you can use a counter caches, how to introduce it in an existent association and some tips to work with…
Already shove a whole second on each app run by un-require rubocop