In those circumstances remember that views and stored procedures are
available - schema_plus is a gem that let's you define views as a part
of your schema.r b file and while stored procedures violate the
ActiveRecord design pattern they can be used with AR and are sometimes
necessary in order to create a system that scales and performs well.
Max
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
Category.includes(: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")
.group("categories.id")
.order("categories.name ASC")
.where("p.gender = 'unisex' OR p.gender = 'male'")
Can you show us the class definitions for Category and Subcategory
(snip the methods).
Colin
That looks ok.
I think the group and order should be category.id and .name rather
than categories but I don't see how that would cause the problem you
are seeing.
Colin
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.subcategory_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 "categories" 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, "categories"."updated_at" AS t0_r3, "categories"."permalink" AS t0_r4, "subcategories"."id" AS t1_r0, "subcategories"."name" ASt1_r1, "subcategories"."category_id" AS t1_r2, "subcategories"."created_at" AS t1_r3, "subcategories"."updated_at" AS t1_r4, "subcategories"."permalink" AS t1_r5 FROM "categories" LEFT OUTER JOIN "subcategories" ON "subcategories"."category_id"= "categories"."id" WHERE (subcategories.id > 0)"
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
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.