我正在使用paranoia gem來「軟刪除」記錄。現在我需要加載這些記錄,其中一些可能已被刪除,用於相關模型。偏執會將此default_scope
的「偏執狂」的模式:急於加載已被刪除的記錄,偏執狂的默認範圍
default_scope :conditions => { :deleted_at => nil }
所以實際上,我有這些(簡體)型號:
class Product
has_many :orders
default_scope :conditions => { :deleted_at => nil }
end
class Order
belongs_to :product
end
我試圖做到的,是對渴望,加載產品時訪問訂單:
Order.includes(:product)
這(從How to use unscoped on associated relations in Rails3?)不會在這裏工作:
Product.unscoped { Order.includes(:product) }
我知道我可以創建一個自定義belongs_to
關係添加條件(如Eager loading nested association and scope),但我不能找到一種方法來去除現有的,如果這甚至有可能。
問題:如何防止默認範圍應用於預先加載查詢?
剛剛發現此相關的問題,這仍然是懸而未決:http://stackoverflow.com/questions/3963124/default-scope-and-associations?rq=1 – Thilo