2012-05-23 24 views
4

在我的Single Table Inheritance模型中,我重写了基本模型中的inherited方法,以便所有后代模型都可以被基本模型的名称识别。以下代码用于为所有继承的类的model_name方法添加重写。STI model_name的弃用警告解决方法

def self.inherited(child) 
    child.instance_eval do 
     def model_name 
     BaesModelDefinition.model_name 
     end 
    end 
    end 

我注意到,这是Rails的3.2.3生产废弃警告:

DEPRECATION WARNING: It looks like something (probably a gem/plugin) is overriding 
the ActiveRecord::Base.inherited method. It is important that this hook executes so 
that your models are set up correctly. A workaround has been added to stop this 
causing an error in 3.2, but future versions will simply not work if the hook is 
overridden. 

有另一种方法,我可以用它来解决这个问题MODEL_NAME?

回答

5

答案结果很简单。只需将super添加到覆盖方法。

def self.inherited(child) 
    child.instance_eval do 
     def model_name 
     BaesModelDefinition.model_name 
     end 
    end 
    super 
    end 
+1

你的意思是超级(孩子)' –

+1

'super'默认会传递params – montrealmike