我知道有三种主要类型的符号在供应条件的其中,
ActiveRecord的方式:
指定和
为其中,
方法是直截了当:
#纯字符串表示法
@人= Person.where(NAME ='尼尔'AND年龄= 27)
#数组符号
@people = Person.where([名称=?AND年龄=?,尼尔,27])
#哈希符号
@people = Person.where({名:尼尔,年龄:27})
指定或
此相同的其中,
方法被绊倒我的哈希语法。这可能吗?
#纯字符串表示法
@人= Person.where(NAME ='尼尔'OR年龄= 27)
#数组符号
@people = Person.where([名称=?OR年龄=?,尼尔,27])
#哈希标记不工作
@people = Person.where({名:尼尔或年龄:27})
解决方案
有4个选项,可以被视为«哈希符号»方式实现(最后是有点散列的 ISH 的)。您可以:
等待Ruby on Rails的5,在这里您将能够做到以下链接:
@people = Person.where(名称:尼尔)
。或(Person.where(年龄:27))
的合并请求请求#16052新增#or到的ActiveRecord ::关联的
Examples
使用where_values加上 注
或 减少
无范围
方法是必要的< A HREF =http://guides.rubyonrails.org/upgrading_ruby_on_rails.html#changes-on-default-scopes>只适合于Rails 4.1+ ,以确保 defaul_scope
不包含在 where_values
(,否则来自predicates default_scope
和其中,
将与链接或
运营商):
条件= Person.unscoped
。凡(名称:尼尔'],年龄:[27])
.where_values
@people = Person.where(conditions.reduce(:或))
安装实现此功能(第三方插件,例如, Squeel ,的