Forum: Ruby on Rails Rails 4 Query on Association using Includes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0506f45fe389025e7028be36514142ea?d=identicon&s=25 mikej (Guest)
on 2016-02-17 17:16
(Received via mailing list)
This works:


Model1.includes(:model2 => :model2).where(model2s: {order:  2})


How would I write a query for order <= 2?


What I would really like to write is:


Model1.includes(:model2 => :model2).where(“model2s.order <= 2”)


This worked in Rails 3, I would like to build up the where clause as a
string.  Any ideas on how I can use a string to query an association in
Rails 4 would be much appreciated.


Many thanks,


Mike
0506f45fe389025e7028be36514142ea?d=identicon&s=25 mikej (Guest)
on 2016-02-24 15:22
(Received via mailing list)
I never worked this out, but you can use joins instead of includes as a
work around.
0506f45fe389025e7028be36514142ea?d=identicon&s=25 mikej (Guest)
on 2016-03-04 13:13
(Received via mailing list)
You can also use:

Model1.includes(:model2 => :model3).where(“model2s.order <=
2”).references(:model2 => :model3)
This topic is locked and can not be replied to.