As Stephen Odonnell said, "... depends on the size of the collection." However, you also need to take into account the number of times that you will do with these calculations on the page . For example, if you are making the βBlogβ application, and the posts / index page displays 10 blog posts, each of which contains β155 commentsβ below, this causes a lot of database queries that can be avoided with a cache counter.
I suggest trying it only with active search for entries, but add the option to "enable". Suppose message # 123 has 50 comments.
p = Post.find_by_id(123) p.comments.each do {|c| puts c }
Will generate 51 database calls, 1 for the message and 1 for each comment. Instead, you can:
Post.find_by_id(123, :include => [:comments])
Now it will automatically request comments when searching for a message, folding 51 requests to 1.
source share