扶手:为ActiveRecord的或查询哈希符号 [英] Rails: Hash notation for ActiveRecord OR query
问题描述
我知道有三种主要类型的符号在供应条件的其中,
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 ::关联的
<一href="https://github.com/rails/rails/blob/master/activerecord/test/cases/relation/or_test.rb">Examples
-
使用<一个href="http://www.rubydoc.info/docs/rails/3.2.8/ActiveRecord%2FQueryMethods%3Awhere_values"><$c$c>where_values$c$c>加上
注
一>或减少
无范围
方法是必要的< A HREF =http://guides.rubyonrails.org/upgrading_ruby_on_rails.html#changes-on-default-scopes>只适合于Rails 4.1+ ,以确保defaul_scope
不包含在where_values
(,否则来自predicatesdefault_scope
和其中,
将与链接或
运营商):条件= Person.unscoped 。凡(名称:尼尔'],年龄:[27]) .where_values @people = Person.where(conditions.reduce(:或))
-
安装实现此功能(第三方插件,例如, Squeel ,的SmartTuple 或 ActiverecordAnyOf )。
-
使用阿雷尔:
TBL = Person.arel_table @people = Person.where(TBL [:名称] .EQ(尼尔)或(TBL。[:年龄] .EQ(27)))
I know there are three main types of notations for supplying conditions in the where
ActiveRecord method:
- Pure String
- Array
- Hash
Specifying and
for the where
method is straight forward:
# Pure String notation
@people= Person.where("name = 'Neil' AND age = 27")
# Array notation
@people = Person.where(["name = ? AND age = ?", 'Neil', 27])
# Hash notation
@people = Person.where({name: "Neil", age: 27})
Specifying or
for this same where
method is stumping me for the hash syntax. Is it possible?
# Pure String notation
@people= Person.where("name = 'Neil' OR age = 27")
# Array notation
@people = Person.where(["name = ? OR age = ?", 'Neil', 27])
# Hash notation DOESN'T WORK
@people = Person.where({name: "Neil" OR age: 27})
There are 4 options that can be considered as implementations of «Hash notation» approach (the last is kinda hash-ish). You can:
-
wait for Ruby on Rails 5, where you will be able to do the following chaining:
@people = Person.where(name: 'Neil') .or(Person.where(age: 27))
Merge Pull Request #16052 Added #or to ActiveRecord::Relation
-
use
where_values
together withinject
orreduce
,unscoped
method is necessary only for Rails 4.1+ to ensuredefaul_scope
is not included in thewhere_values
(otherwise predicates from bothdefault_scope
andwhere
would be chained withor
operator):conditions = Person.unscoped .where(name: ['Neil'], age: [27]) .where_values @people = Person.where(conditions.reduce(:or))
-
install third-party plugins that implement this feature (for example, Squeel, SmartTuple or ActiverecordAnyOf).
-
use Arel:
tbl = Person.arel_table @people = Person.where(tbl[:name].eq('Neil').or(tbl[:age].eq(27)))
这篇关于扶手:为ActiveRecord的或查询哈希符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!