What's the best way to optimize a Rails app where you need to pull info from the database for a collection of items?
For example, if I am building an online forum system... I would have a number of Posts
and the posts would have_many :recommendations
and :comments
and :favorites
. When I display the posts in my view, I might need to display this info - like a count of how many comments there are, and whether or not my current user has favorited/recommended the post.
I was playing around with this and it seems there are a huge number of DB queries being executed! First I'm grabbing my collection of posts - then, iterating through each one, I am going to the comments table, the favorites table, etc... to get counts or see if records exist for that particular post. So for every post I have, I'm executing 3 separate queries!
Is there a more efficient way to get this data, or are tons of queries like this normal for a scenario like I describe?