Consider these scopes on an ActiveRecord model:
scope :onsale, where(:sales_state => "onsale")
scope :outdated, where(:sales_state => "outdated")
scope :onsale_or_outdated, where(" sales_state = 'onsale' OR sales_state = 'outdated' ")
Why is it that Rails knows to override :onsale
with :outdated
, but not :onsale
with :onsale_or_outdated
?
My use case:
I have a relation object with many scopes (let's say it's a saved search), and one of those is :onsale
. I want to make another relation starting from that, but this time I want the sales_state
to be either onsale
or outdated
.
If I use relation.onsale_or_outdated
, the sales_state
is not overridden, it just adds a new condition.
[...] WHERE "cars"."sales_state" = 'onsale' [...] AND (("cars"."sales_state" = 'onsale' OR "cars"."sales_state" = 'outdated'))
How can I use my 'or'-ed condition in this context?