Skip to content

Instantly share code, notes, and snippets.

@zaiste
Last active May 3, 2018 20:34
Show Gist options
  • Save zaiste/72ab378402b4aed84372f08e79fbcdba to your computer and use it in GitHub Desktop.
Save zaiste/72ab378402b4aed84372f08e79fbcdba to your computer and use it in GitHub Desktop.
Rails 5: OR operator in Active Record

Rails 5: OR operator in Active Record

Rails 5 allows to use or operator when building Active Record queries.

Article.where(id: 9)
  .or(Article.where(title: 'Rails 5: OR operator in Active Record'))

The query above returns a union of two relations specified. Those relations must be structurally compatible: they must use the same model and set parameters via WHERE or HAVINGb. Additionally, none of the relations can use .limit(), .offset() or .distinct() functions.

This Active Record query is converted to the following SQL query.

SELECT "articles".* FROM "articles" 
WHERE (
"articles"."id" = ? 
OR 
"articles"."title" = ?)  
[
    ["id", 9], 
    ["title", "Rails 5: OR operator in Active Record"]
]

or operator can be combined with group and having queries.

articles = Article.group(:author_id)
articles.having('id > 8')
    .or(articles.having('title like "Rails 5%"'))

This Active Record query is converte to the following SQL query.

SELECT "articles".* 
FROM "articles" 
GROUP BY "articles"."author_id" 
HAVING ((id > 8) OR (title like "Rails 5%"))

or operator can be combined with existing scopes

class Article < ApplicationRecord
  scope :published, -> { where(published: true) }
end

Article.published
  .or(Article.where('id > 8'))

or operator can be also used to combine two or more scopes togther

class Article < ApplicationRecord
  scope :status, -> (value) { where(status: value) }
  scope :published, -> { where(accepted: true) }
  scope :ready, -> { status("accepted").or(published) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment