我有一个简单的查询,我正在尝试构建。
我想将参数传递给查询。
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!
任何人都知道如何传递参数化查询的值?
答案 0 :(得分:5)
您可以使用*
将数组转换为参数列表:
fields = ['id = ?','type_id = ?','brand_id = ?']
values = [1, 1, 1]
Product.where(fields.join(' and '), *values)