Is there any way to override the default scope or previous scope in a where condition? (Beside re-structuring the model not to use a default_scope
or use unscoped)
Also, what is the reason why this works the way it does? It feels like it is not the most expected or intuitive approach.
Sample:
class Product < ActiveRecord::Base
default_scope -> { visible }
scope :visible, -> { where(visible: true) }
scope :hidden, -> { where(visible: false) }
end
When I do this:
Product.hidden
The generated sql tries to match by two values:
WHERE "products"."visible" = 't' AND "products"."visible" = 'f'
The same goes without default_scope:
class Product < ActiveRecord::Base
scope :visible, -> { where(visible: true) }
scope :hidden, -> { where(visible: false) }
end
When I do this:
Product.visible.where(visible: false)
Or when I do this:
Product.visible.hidden
The generated sql tries to match by two values:
WHERE "products"."visible" = 't' AND "products"."visible" = 'f'
I made this gist with a complete test case: https://gist.github.com/mmontossi/dcf71457e98a169c28a5
This is the issue when I first asked about this thinking it was a bug: https://github.com/rails/rails/issues/20907#issuecomment-122131096