在我的单表继承模型中,我重写了基本模型中的inherited
方法,以便所有后代模型都可以被基本模型的名称识别。以下代码用于为所有继承的类添加对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问题吗?
答案 0 :(得分:5)
答案结果很简单。只需在覆盖方法中添加super
即可。
def self.inherited(child)
child.instance_eval do
def model_name
BaesModelDefinition.model_name
end
end
super
end