开发者

HABTM based find without hard-coded SQL fragments

开发者 https://www.devze.com 2023-02-17 03:39 出处:网络
Order habtm devices and devices habtm orders. I need to find ord开发者_如何学编程ers, having ALL specified devices

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})
0

精彩评论

暂无评论...
验证码 换一张
取 消