When 开发者_如何学Gowriting 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.
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
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.
精彩评论