2011-08-30 81 views
10

On Rails的3.1 RC6,給予ActiveRecord的STI:我怎樣才能擺脫父類的默認範圍

class Animal < ActiveRecord::Base 
    default_scope where(legs: 4) 
end 

下不能按預期工作:

class Man < Animal 
    default_scope unscoped.where(legs: 2) 
end 

產生的SQL語句看起來像這樣:

SELECT * FROM animals WHERE legs = 4 AND legs = 2 

我怎樣才能完全重寫父類的默認範圍?

我也試過以下其中沒有工作:

default_scope{ unscoped.where legs: 2 } 
default_scope with_exclusive_scope{ legs: 2 } 

回答

8

我挖到Rails的源代碼,並與下的Rails 3.1有效的解決方案提出了(測試用ActiveRecord 3.1.0.rc6 ):

class Animal < ActiveRecord::Base 
    default_scope where(legs: 4) 
end 

class Man < Animal 
    self.default_scopes = [] 
    default_scope where(legs: 2) 
end 
+0

看來,這部作品在Rails的3.0:self.default_scoping = [] – jemminger

相關問題