4

我正在使用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),但我不能找到一種方法來去除現有的,如果這甚至有可能。

問題:如何防止默認範圍應用於預先加載查詢?

+0

剛剛發現此相關的問題,這仍然是懸而未決:http://stackoverflow.com/questions/3963124/default-scope-and-associations?rq=1 – Thilo

回答

1

嘛,原來的解決方法是給力的「偏執狂」的模式,規避了default_scope聯接:

Order.joins(:product).includes(:product) 

不漂亮,但它的作品。如果可能的話,希望有更好的答案。