Technology
A counter cache is a handy way to speed up queries where you only need the number of an associated model rather than the entire collection. In your code, you might see something like this: @post.comments.count When you run this code, you wind up with another query to the database to get that number. That's not a big deal until you have that count being called for each post in your stream. To avoid this, you can use a counter cache. To use the counter cache, add a _count to the primary model's table. In this example, it would be called "comments_count". class AddCounterCacheToPosts < ActiveRecord::Migration def change add_column :posts, :counter_cache, :integer end end With that migration in place, all you need to do from there is add :counter_cache => true to your other model—in this case Comment. class Comment < ActiveRecord::Base belongs_to :post, :counter_cache => true end Now, whenever you create or delete a new comment for a post, it'll update that counter_cache column on the posts table. A few things to keep in mind are: If you're adding this to an existing system, you'll probably have to add a data migration to set the counts. This number is not checked each time it's changed, so if you change the number, you'll get results in the future relative to that number.