I'm using Rails3, ActiveRecord
Just wondering how can I chain the scopes with OR statements rather than AND.
e.g.
Person.where(:name => "John").where(:lastname => "Smith")
That normally returns name = 'John' AND lastname = 'Smith', but I'd like:
name = 'John' OR lastname = 'Smith'
Use ARel
If you can't write out the where clause manually to include the "or" statement (ie, you want to combine two scopes), you can use union:
(source: ActiveRecord Query Union)
This is will return all records matching either query. However, this returns an array, not an arel. If you really want to return an arel, you checkout this gist: https://gist.github.com/j-mcnally/250eaaceef234dd8971b.
This will do the job, as long as you don't mind monkey patching rails.
You can also use MetaWhere gem to not mix up your code with SQL stuff:
An updated version of Rails/ActiveRecord may support this syntax natively. It would look similar to:
As noted in this pull request https://github.com/rails/rails/pull/9052
For now, simply sticking with the following works great:
Update for Rails4
requires no 3rd party gems
Old answer
requires no 3rd party gems