2013-04-01 25 views
3

ActiveRecord是否支持where子句中的元组,假设底层数据库呢?Ruby ActiveRecord和sql元组支持

产生的where子句看起来是这样的:

where (name, address) in (('John', '123 Main St')) 

我想:

Person.where({[:name, :address] => ['John', '123 Main St']}) 

,并没有奏效。

回答

1
Person.where("(name, address) IN ((?))", ['John', '123 Main St']) 
+0

与一个元组的工作,但不能与多个元组。我尝试过'[['John','123 Main St'],['Jane','456 Oak St']],但AR将每个嵌套数组转换为YAML。 – Kelvin

0
tupleArray = [['John', '123 Main St'],['Jane', '124 Main St']] 
Person.where("(name, address) IN (#{(['(?)']*tupleArray.size).join(', ')})", *tupleArray) 
+0

这是一个黑客攻击,并没有资格被ActiveRecord“支持”。另外,黑客已经在[这里]提到(https://groups.google.com/d/msg/rubyonrails-core/RnKkEwbcNCk/3AJz9UOy_d4J) – Kelvin