“Here is on how to handle Rails 5 ActiveRecord OR query chain.”
So it’s been a while I have been playing with Rails 5 and I have noticed several changes from Rails 4 and of course Rails 3.2.
I have been working with ActiveRecord and tried to construct an OR chained query. As I have been checking the Rails 5 documentation I ended up on a github pull request in which the Rails contributors were discussing the ability to chain two queries.
An example of would be:
Worker.where(name: 'Mark').or(Worker.where(supervisor: true))
where it will return the worker whose name is Mark or whoever is a supervisor.
Here are some other examples:
Finding the worker that is named Mark and is 55 years old or named Chris:
Worker.where(name: 'Mark').where(age: 55).or(Worker.where(name: 'Chris'))
Finding the worker that is named Chris or Mark and is supervisor:
Worker.where(name: 'Chris').or(Worker.where('Mark')).where(supervisor: true)
Questions? Feel free to ask away!