在rails上的ruby中,如何将数组作为一系列参数传递

时间:2013-01-22 06:16:59

标签: ruby-on-rails parameters rails-activerecord

我有一个简单的查询,我正在尝试构建。
我想将参数传递给查询。

Product.where('id = ?', 1)

轻松正确.. 如果字段列表和值是数组

,该怎么办
fields = ['id = ? ', 'type_id = ?', 'brand_id = ?']
values = [1, 1, 1]
Product.where(fields.join(' and '), values)  #does not compute, does not compute!

任何人都知道如何传递参数化查询的值?

1 个答案:

答案 0 :(得分:5)

您可以使用*将数组转换为参数列表:

fields = ['id = ?','type_id = ?','brand_id = ?']
values = [1, 1, 1]
Product.where(fields.join(' and '), *values)