Rails 5: OR operator in Active Record

2018-02-03 ·

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 HAVING. 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
Copyright © 2018 All rights reserved.
Powered by Kulfon.