Friday, January 6, 2012

Re: [Rails] Complex query with multiple joins

Wow! Thank you for all the help Peter! I really appreciate it. I will test that code tomorrow as it's getting quite late here.

I did a small test now though. It seems that you can't mix joins and includes.

This doesn't work:
Category.includes(:subcategories).joins("INNER JOIN resellercategories AS r ON subcategories.id = r.s
ubcategory_id").where("subcategories.id > 0")

"SELECT "categories".* FROM "categories" INNER JOIN resellercategories AS r ON subcategories.id = 
r.subcategory_id WHERE (subcategories.id > 0)
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: subcategories.id: SELECT "categories".* FROM "categ
ories" INNER JOIN resellercategories AS r ON subcategories.id = r.subcategory_id WHERE (subcategories.id > 0)"


But this do work:
Category.includes(:subcategories).where("subcategories.id > 0")

"SELECT "categories"."id" AS t0_r0, "categories"."name" AS t0_r1, "categories"."created_at" AS t0_r2, "categ
ories"."updated_at" AS t0_r3, "categories"."permalink" AS t0_r4, "subcategories"."id" AS t1_r0, "subcategories"."name" AS 
t1_r1, "subcategories"."category_id" AS t1_r2, "subcategories"."created_at" AS t1_r3, "subcategories"."updated_at" AS t1_r
4, "subcategories"."permalink" AS t1_r5 FROM "categories" LEFT OUTER JOIN "subcategories" ON "subcategories"."category_id"
 = "categories"."id" WHERE (subcategories.id > 0)"


Quite odd I think...

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/-/xZ9H3t_aQz8J.
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