baa*_*h05 1 parameters ruby-on-rails rails-activerecord
我有一个简单的查询,我正在尝试构建.
我想将参数传递给查询.
Product.where('id = ?', 1)
Run Code Online (Sandbox Code Playgroud)
轻松如右..如果字段列表和值是数组,该怎么办
fields = ['id = ? ', 'type_id = ?', 'brand_id = ?']
values = [1, 1, 1]
Product.where(fields.join(' and '), values) #does not compute, does not compute!
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何传递参数化查询的值?
您可以使用*将数组转换为参数列表:
fields = ['id = ?','type_id = ?','brand_id = ?']
values = [1, 1, 1]
Product.where(fields.join(' and '), *values)
Run Code Online (Sandbox Code Playgroud)