How to bypass default_scope

Today I learned how to bypass default_scope using ActiveRecord::QueryMethods#unscope. And change a previously set where condition.

class Products
  default_scope where(enabled: true)
  scope :disabled, -> { unscope(enabled: false) }
end

Another helpful method is ActiveRecord::QueryMethods#rewhere

class Products
  default_scope where(enabled: true)
end

product = Products.first
product.rewhere(enabled: false)

Leave a Reply

Your email address will not be published. Required fields are marked *

Up Next:

Force string to boolean object

Force string to boolean object