Does Rails 4 have support for OR queries

Ruby on-RailsRuby on-Rails-4

Ruby on-Rails Problem Overview


So in Rails 4 the long desired feature to use not queries has been added.

Article.where.not(title: 'Rails 3')

Has similar support been added for or queries, or are they planning to make it. I couldn't find anything by browsing through the release notes.

Obviously I tried

Article.where(title: 'Rails 3').or(title: 'Rails 4')

But that does not work.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Article.where(title: ['Rails 3', 'Rails 4'])

is how you'd do that in Active Record.

It's not possible to replicate any arbitrary SQL query using "Rails-y" syntax. But you can always just pass in literal sql.

So you could also do:

Article.where("articles.title = 'Rails 3' OR articles.title = 'Rails 4'")

Solution 2 - Ruby on-Rails

This now works in Rails 5:

Article.where(title: 'Rails 3').or(Article.where(title: 'Rails 4'))

Code example in Rails's source.

Solution 3 - Ruby on-Rails

The most common alternative is already answer by @gregates

Recently there has been pull request in rails source

Add #any_of query method to active_record Which adds or functionality to activerecord

the contributor has already created a gem incase its not accepted

its rails 3.2 and 4 compatible

https://github.com/oelmekki/activerecord_any_of

I havent tried it yet but soon wish to use it looks good to me.

Solution 4 - Ruby on-Rails

I know that this is an old thread, but anyone else looking for a solution to this problem might find this code helpful:

Article.where(title: ["Rails 3", "Rails 4"])

Maybe that will help you get someone going in the right direction without having to risk sql injection.

Solution 5 - Ruby on-Rails

It seems that the rails master branch is now supporting OR queries. https://github.com/rails/rails/pull/16052

I assume it will be in the framework with the next major release.

Solution 6 - Ruby on-Rails

Rails 5 will support it but you can use this backport for Rails 4.2 : https://github.com/Eric-Guo/where-or

Solution 7 - Ruby on-Rails

You can use Squeel for more complex queries:

Article.where{(title == 'Rails 3') | (title == 'Rails 4')}

This results in the following SQL query:

SELECT `articles`.* FROM `articles` WHERE ((`articles`.`title` = 'Rails 3' OR `articles`.`title` = 'Rails 4'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArjanView Question on Stackoverflow
Solution 1 - Ruby on-RailsgregatesView Answer on Stackoverflow
Solution 2 - Ruby on-RailsFellow StrangerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsPritesh JainView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRob RicheyView Answer on Stackoverflow
Solution 5 - Ruby on-RailsArjanView Answer on Stackoverflow
Solution 6 - Ruby on-RailsNicolas MaloeuvreView Answer on Stackoverflow
Solution 7 - Ruby on-RailsStefanView Answer on Stackoverflow