Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use unscoped on associated relations in Rails3?

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.

like image 793
crispy Avatar asked Jan 21 '11 11:01

crispy


3 Answers

Oh. I fooled myself. Thought the following would not work... but it does:

Product.unscoped do
  my_photo.product
end

Notice that you have to call unscoped on the model with the default_scope that should be bypassed.

Also, inheritance has to be respected. If you have class InsuranceProduct < Productand class FinancialProduct < Product and a default_scope in Product, all of the following two combinations will work:

InsuranceProduct.unscoped do
  my_record.insurance_products
end

FinancialProduct.unscoped do
  my_record.financial_products
end

Product.unscoped do
  my_record.products
end

However, the following will not work although the scope is defined in Product:

Product.unscoped do
  my_record.financial_products
end

I guess that's another quirk of STI in Ruby / Rails.

like image 76
crispy Avatar answered Nov 08 '22 02:11

crispy


Another option is to override the getter method and unscope super:

class Photo < ActiveRecord::Base
  belongs_to :product

  def product
    Product.unscoped{ super }
  end
end

I ran into the same situation where I had one associated model that needed to be unscoped, but in almost every other case it needed the default scope. This should save you the extra calls to unscoped if you are using the assocation getter in more than one place.

like image 59
johnmcaliley Avatar answered Nov 08 '22 02:11

johnmcaliley


I'm probably a bit late to the party, but some time ago I found myself in the same situation and I wrote a gem to do this easily: unscoped_associations.

Usage:

belongs_to :user, unscoped: true

Support for:

  • belongs_to
  • has_one
  • has_many

Polymorphic associations are also supported.

like image 31
markets Avatar answered Nov 08 '22 01:11

markets