3
概念上我有2個型號,一個帶有默認範圍ActiveRecord的查詢與包括避免了關係默認範圍
class Model
default_scope where: "is_acitve = 1"
end
class SpecialUser
has_many :model
## 1
def model
Model.unscoped { super }
end
end
我想有幾個選擇地重寫對模型的默認範圍 - 很多模型的其他用戶存在,他們都應該只看到活動的模型。只有少數特殊情況才能訪問不活動的案例。
採用方法#1,我可以處理這種情況
s = SpecialUser.find_by_id x
s.model # <-- works for even is_active =0 cases.
但是,如果我試圖像下面(的表現):
s = SpecialUser.includes(:model).where("id = 5")
默認範圍注入查詢。
任何避免默認範圍的方法,即使使用includes?
這是軌道3.2.14 – aabes