Saturday, January 7, 2012

Re: [Rails] Complex query with multiple joins

Hi again!

I posted a question about this issue on StackOverflow and got an interesting response. To force Rails to join the included table you can use eager_load() instead of includes(). Using eager_load() and removing the group() seems to make my query work perfect.

Correct version:

Category.eager_load(:subcategories)
             .joins("INNER JOIN resellercategories AS r ON subcategories.id = r.subcategory_id")
             .joins("INNER JOIN products AS p ON r.id = p.resellercategory_id")
             .order("categories.name ASC")
             .where("p.gender = 'unisex' OR p.gender = 'male'")


Here's the discussion at SO:
http://stackoverflow.com/questions/8771304/complex-query-use-includes-and-joins-at-the-same-time

Thanks for all the help!

Best Regards
Linus

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/5-A3Qp0qLu8J.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate