0

I have a Car model like this.

class Car < ActiveRecord::Base
    scope :modern, -> do
        where(make_date: 2018)
    end
    scope :black, -> do
        where(color: :black)
    end
    scope :bmw, -> do
        where(make: :bmw)
    end
end

I can find all modern cars by Car.modern.

I can find all black cars by Car.black.

I can find all BMW cars by Car.bmw.

How do I use these named scopes to find all cars that are either 'black' or 'modern' ? So that i can have Old-Black-Nissan, Modern-White-BMW, Moder-black-Ford all in a single resultset.

Using Rails 4.2.8

Rakib
  • 12,376
  • 16
  • 77
  • 113
  • The answer is different depending on which version of rails you are using (>=5 or <5). – jvillian Feb 23 '18 at 19:05
  • using Rails 4.2.8 – Rakib Feb 23 '18 at 19:05
  • 2
    Possible duplicate of [Rails: How to chain scope queries with OR instead of AND?](https://stackoverflow.com/questions/3684311/rails-how-to-chain-scope-queries-with-or-instead-of-and) – Gerry Feb 23 '18 at 20:08
  • If you could upgrade to rails 5, this version has added support for OR in ActiveRecord. http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-or – Pablo Feb 24 '18 at 12:06

1 Answers1

-1

You can use this gem. More info there. Please search thoroughly before asking questions yourself. Most likely someone stumbled upon that problem before.