rails/rails — pull request #9052 by gaelmuller (2013-01-23 23:40:52+00:00)
Title: Added #or to ActiveRecord::Relation

ActiveRecord::Relation#or returns a new relation, which is the
result of filtering the current relation according to the
conditions in the arguments, joining WHERE clauses with OR
operand, contraty to the default behaviour that uses AND.

ActiveRecord::Relation#or accepts conditions in one of several
formats. In the examples below, the resulting SQL is given as an
illustration; the actual query generated may be different depending
on the database adapter.

* without arguments

If ActiveRecord::Relation#or is used without arguments, it returns
an ActiveRecord::OrChain object that can be used to chain queries
with any other relation method, like where:

Post.where("id = 1").or.where("id = 2")
> SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'id = 2'))

It can also be chained with a named scope:

Post.where("id = 1").or.containing_the_letter_a
> SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'body LIKE \\'%a%\\''))

* ActiveRecord::Relation

When #or is used with an ActiveRecord::Relation as an argument, it
merges the two relations, with the exception of the WHERE clauses,
that are joined using the OR operand.

Post.where("id = 1").or(Post.where("id = 2"))
> SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'id = 2'))

* anything you would pass to #where

ActiveRecord::Relation#or also accepts anything that could be passed
to the #where method, as a shortcut:

Post.where("id = 1").or("id = ?", 2)
> SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'id = 2'))