I have a default scope on products due to information security constraints.
class Product < ActiveRecord::Base
has_many :photos
default_scope where('visible = 1')
end
In my associated Photo model, however, I also have to find products that should not be visible.
class Photo < ActiveRecord::Base
belongs_to :product
end
my_photo.product
In other cases, I can use unscoped in order to bypass the default_scope, e.g. in Product.unscoped.find_by_title('abc'). However:
How to remove the scope when using associations of a record?
my_photo.unscoped.product does not make sense as my_photo does not have a method called unscoped. Neither does my_photo.product.unscoped make sense as my_photo.product may already be nil.
Oh. I fooled myself. Thought the following would not work… but it does:
Notice that you have to call unscoped on the model with the
default_scopethat should be bypassed.Also, inheritance has to be respected. If you have
class InsuranceProduct < Productandclass FinancialProduct < Productand adefault_scopeinProduct, all of the following two combinations will work:However, the following will not work although the scope is defined in
Product:I guess that’s another quirk of STI in Ruby / Rails.