7

我見過很多關於此的帖子,但似乎沒有解決我的問題。我在模型上有一個default_scope像這樣:Rails 3.1.3無人機範圍

default_scope where(:is_active => true).order('LOWER(table.name)'); 

我還有其他的(正常)範圍,我想創建一個使用unscopedinactive範圍。我想將它定義爲一個範圍,但如果定義爲類方法,它僅適用:

# 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 

有沒有辦法,我錯過了一個辦法,還是我使用一個類的方法來定義這個範圍?

+0

類似於http://stackoverflow.com/questions/6919307/rails-3-default-scope-scope-with-overrride – 2012-01-04 22:31:17

+0

(如果您閱讀那裏的評論,並沒有真正的解決方案) – 2012-01-04 22:31:41

+0

我會說,除了''和'only'外,他們似乎並不否定'default_scope'在另一個範圍內,類似於'unscoped'的問題,並且'with_exclusive_scope'。 – 2012-01-04 22:38:07

回答

6

似乎有沒有一個方法可以做到這一點。我在github的rails repo上打開了一個問題...

+2

作爲解決方法,您可以創建一個使用unscoped的類方法,該方法可以正常工作。 – 2012-08-29 19:09:07

+1

僅供參考:https://github.com/rails/rails /問題/ 4306 – 2013-11-30 11:19:36

0

試試這個

scope :inactive, lambda { unscoped.where(:is_active => false) } 
+0

也不工作 – sethvargo 2012-01-04 22:10:39

+1

但是我把它添加到我的未嘗試的可能性列表 – sethvargo 2012-01-04 22:12:19

+0

ftr,既沒有不顯眼{lambda {...}} – sethvargo 2012-01-04 22:12:46