OR-ing multiple named scopes in Ruby on Rails ActiveRecords

70 Views Asked by At

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

1

There are 1 best solutions below

0
Artur Martsinkovskyi On

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