Order habtm devices and devices habtm orders.
I need to find ord开发者_如何学编程ers, having ALL specified devices
I'm doing following:
devices = Device.all :conditions => {:name => params[:devices].split(",")}
@orders = Order.all :joins => :devices, :conditions => {:devices => devices}
It produces following SQL:
SELECT "orders".*
FROM "orders"
INNER JOIN "orders_devices" ON "orders_devices".order_id = "orders".id
INNER JOIN "devices" ON "devices".id = "orders_devices".device_id
WHERE ("orders"."devices" IN (110330561,530240381)) ORDER BY date DESC)
The last string of the query is incorrect, and sure, i get an error:
SQLite3::SQLException: no such column: orders.devices
Why i'm getting this result ?
How can i solve the problem, without specifying SQL query fragments, like that:
:conditions => ['devices.id in (?)',[1,2]]
Maybe just this?
@orders = Order.all(:joins => :devices, :conditions => {"devices.id" => devices})
精彩评论