2

When writing activerecord queries, I typically just use regular sql syntax [field_a = 'b' OR field_c = 'd'] when I need to use "or" in query. I'm just wondering if there's a way to do it without resorting to sql? Have tried googling 'activerecord or' but, unsurprisingly, not much joy.

Any suggestions appreciated.

PlankTon
  • 12,443
  • 16
  • 84
  • 153

2 Answers2

2

It's not possible (yet) using ActiveRecord alone. The parts of the relation are always ANDed together, so the only option is to use a SQL string for that part of the .where()

Otherwise you can resort to Arel for now: ActiveRecord OR query

Community
  • 1
  • 1
Jo P
  • 1,656
  • 21
  • 25
0

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))

You can check the documentation and the examples.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115