74

I know there are 3 main notations for supplying arguments to the where ActiveRecord method:

  1. Pure String
  2. Array
  3. Hash

Specifying and for the where method is straight forward:

# Pure String notation
Person.where("name = 'Neil' AND age = 27")

# Array notation
Person.where(["name = ? AND age = ?", 'Neil', 27])

# Hash notation
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
Person.where("name = 'Neil' OR age = 27")

# Array notation
Person.where(["name = ? OR age = ?", 'Neil', 27])

# Hash notation DOESN'T WORK
Person.where({name: "Neil" OR age: 27})
potashin
  • 44,205
  • 11
  • 83
  • 107
Neil
  • 4,578
  • 14
  • 70
  • 155

2 Answers2

144

There are 5 options that could be considered as implementations of «Hash notation» (the last two are kinda hash-ish):

  1. With Ruby on Rails 5 you are able to do the following chaining using ActiveRecord::Relation#or method:

    Person.where(name: 'Neil').or(Person.where(age: 27))
    
  2. Use where_values together with reduce. The unscoped method is necessary only for Rails 4.1+ to ensure default_scope is not included in the where_values. Otherwise predicates from both default_scope and where would be chained with the or operator:

    Person.where( 
      Person.unscoped.where(name: ['Neil'], age: [27]).where_values.reduce(:or) 
    )
    
  3. Install third-party plugins that implement these or similar features, for example:

    • Where Or (backport of the Ruby on Rails 5 .or feature mentioned above)

    • Squeel

      Person.where{(name == 'Neil') | (age == 27)} 
      
    • RailsOr

      Person.where(name: 'Neil').or(age: 27)
      
    • ActiverecordAnyOf

      Person.where.anyof(name: 'Neil', age: 27)
      
    • SmartTuple

      Person.where(
        (SmartTuple.new(' or ') << {name: 'Neil', age: 27}).compile
      )
      
  4. Use Arel:

    Person.where( 
      Person.arel_table[:name].eq('Neil').or(
        Person.arel_table[:age].eq(27)
      ) 
    )
    
  5. Use prepared statements with named parameters:

    Person.where('name = :name or age = :age', name: 'Neil', age: 27)
    
potashin
  • 44,205
  • 11
  • 83
  • 107
  • 1
    This is a complete answer. The `arel` answer is particularly good since this is what's getting added to Rails 5, as I understand. – Tom Harrison Jan 16 '16 at 21:51
  • If you can't wait for Rails 5, you can use this backport of the feature to rails 4.2. Nicely packaged up as the 'where-or' gem https://github.com/Eric-Guo/where-or – Rob Jun 13 '16 at 17:27
  • If your project is still in Rails 3+, you can try the rails_or gem: https://github.com/khiav223577/rails_or – khiav reoy Jan 25 '17 at 04:20
1

As says potashin, you can use another third-party plugins that implement this feature. I have a long time using Squeel and works pretty well for this and much more features like complex subqueries or joins.

That query using squeel:

@people= Person.where{(name == 'Neil') | (age = 27)}