基于 HABTM 的查找,无需硬编码 SQL 片段
订购 habtm 设备和 habtm 设备订单。
我需要查找订单,具有
我正在执行的所有指定设备:
devices = Device.all :conditions => {:name => params[:devices].split(",")}
@orders = Order.all :joins => :devices, :conditions => {:devices => devices}
它会生成以下 SQL:
选择“订单”。*
来自“订单”
内连接“orders_devices”ON“orders_devices”.order_id =“orders”.id
内连接“devices”ON“devices”.id =“orders_devices”.device_id
WHERE ("orders"."devices" IN (110330561,530240381)) ORDER BY date DESC)
查询的最后一个字符串不正确,当然,我收到错误:
SQLite3::SQLException:没有这样的列:orders.devices
为什么我得到这个结果?
如何在不指定 SQL 查询片段的情况下解决问题,如下所示:
:conditions => ['devices.id in (?)',[1,2]]
Order habtm devices and devices habtm orders.
I need to find orders, 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]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许只是这个?
Maybe just this?