026 RC Counter Caches




Rails Coach show

Summary: 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