2013-04-30 105 views
0

如何在Rails中编写where子句以测试多个值?Where子句的Rails语法

这不起作用:

where("wostatus_id = ?", 231 or 230 or 8466) 

还需要语法是这样的:

where("wostatus_id != ?", 231 and 230 and 8466) 

感谢您的帮助!

回答

4

你可以这样做......

where("wostatus_id IN (?)", [231, 230, 8466]) 

where("wostatus_id NOT IN (?)", [231, 230, 8466]) 
+0

感谢您的帮助! – Reddirt 2013-04-30 18:11:41

2

你可以使用正常的hash condition

Model.where(wostatus_id: [230, 231, 8466]) 

这将产生预期的 “IN”查询。在Rails 3中,没有办法将其转换为“NOT IN”,因此您需要回到where("wostatus_id NOT IN (?)", [230, 231, 8466])方法。

在Rails 4,你应该能够做到:

Model.where.not(wostatus_id: [230, 231, 8466]) 

参见:How to express a NOT IN query with ActiveRecord/Rails?

+0

谢谢 - 这是很好的信息。 – Reddirt 2013-05-01 14:51:09