Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

ruby on rails - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...