开发者

Using :include and :conditions creates big queries?

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜