Search code examples
ruby-on-railsruby-on-rails-4

Does Rails 4 have support for OR queries


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.


Solution

  • 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'")