I want to fetch all the posts which are commented by specifc user with the eager loading of creator,editor,comments and forum.
Pos开发者_Python百科t.rb
named_scope :list_of_post {|user_id|
{
:include => [:creator,:editor,:comments,:forum],
:conditions => ["comments.user_id = ?",user_id]
}
}
But this named scope creates a big query instead of small IN queries for creator,editor,comments and forum.
This is happening because of using :include and :conditions together.
Any solutions which returns IN queries for each instead of one big query.
I assume you're using rails 2 You could do that instead of a scope :
Post.find(:conditions => {:id => user.comments.map(:post_id)}, :include => [...])
When you migrate to rails 3, look at squeel and ransack. It could help you to do your requests easier.
What happens when you throw in a :joins => :comments
in there? It has to join to do the search, but would explicitly setting the join pull the other tables out of the query?
精彩评论