扶手:为ActiveRecord的或查询哈希符号 [英] Rails: Hash notation for ActiveRecord OR query

查看:179
本文介绍了扶手:为ActiveRecord的或查询哈希符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有三种主要类型的符号在供应条件的其中, ActiveRecord的方式:

  1. 纯字符串
  2. 阵列
  3. 哈希

指定其中,方法是直截了当:

#纯字符串表示法 @人= 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 的)。您可以:

       
  1.       

    等待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       

       
  2.    
  3.       

    使用<一个href="http://www.rubydoc.info/docs/rails/3.2.8/ActiveRecord%2FQueryMethods%3Awhere_values"><$c$c>where_values加上 减少 无范围 方法是必要的< A HREF =htt​​p://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(:或))
    

       

  4.    
  5.       

    安装实现此功能(第三方插件,例如, Squeel ,的SmartTuple ActiverecordAnyOf )。       

       
  6.    
  7.       

             使用阿雷尔:       

     

      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:

  1. Pure String
  2. Array
  3. 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:

  1. 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

    Examples

  2. use where_values together with inject or reduce, unscopedmethod is necessary only for Rails 4.1+ to ensure defaul_scope is not included in the where_values(otherwise predicates from both default_scope and where would be chained with or operator):

    conditions = Person.unscoped
                       .where(name: ['Neil'], age: [27])
                       .where_values
    @people = Person.where(conditions.reduce(:or))
    

  3. install third-party plugins that implement this feature (for example, Squeel, SmartTuple or ActiverecordAnyOf).

  4. use Arel:

    tbl = Person.arel_table
    @people = Person.where(tbl[:name].eq('Neil').or(tbl[:age].eq(27)))
    

这篇关于扶手:为ActiveRecord的或查询哈希符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆