我見過很多關於此的帖子,但似乎沒有解決我的問題。我在模型上有一個default_scope
像這樣:Rails 3.1.3無人機範圍
default_scope where(:is_active => true).order('LOWER(table.name)');
我還有其他的(正常)範圍,我想創建一個使用unscoped
的inactive
範圍。我想將它定義爲一個範圍,但如果定義爲類方法,它僅適用:
# works
def self.inactive
unscoped { where(:is_active => false) }
end
# none of these work
scope :inactive, unscoped { where(:is_active => false) }
scope :inactive, with_exclusive_scope { where(:is_active => true) }
scope :inactive, unscoped.where(:is_active => false)
scope :inactive, lambda { unscoped { where(:is_active => false) } }
scope :inactive, unscoped { lambda { where(:is_active => false) } }
unscoped do
scope :inactive, where(:is_active => false)
end
有沒有辦法,我錯過了一個辦法,還是我使用一個類的方法來定義這個範圍?
類似於http://stackoverflow.com/questions/6919307/rails-3-default-scope-scope-with-overrride – 2012-01-04 22:31:17
(如果您閱讀那裏的評論,並沒有真正的解決方案) – 2012-01-04 22:31:41
我會說,除了''和'only'外,他們似乎並不否定'default_scope'在另一個範圍內,類似於'unscoped'的問題,並且'with_exclusive_scope'。 – 2012-01-04 22:38:07