Skip to content

Latest commit

 

History

History
26 lines (19 loc) · 845 Bytes

active-record-query-for-this-or-that.md

File metadata and controls

26 lines (19 loc) · 845 Bytes

ActiveRecord Query For This Or That

When including multiple where clauses on a query, we are adding more specificity to the resulting ActiveRecord relation -- it's like saying we want records that match this and that. But what about when we want to find records that match this or that?

This is supported by ActiveRecord through the or query method.

Let's say we want all books that are either unpublished or are published in 2019.

> Book.where(status: 'unpublished').or(Book.where(publication_year: 2019))
=> #<ActiveRecord::Relation [...]>

This will generate SQL that includes a where clause like the following:

where (books.status = 'unpublished' or books.publication_year = 2019)

See the docs for more details.