在我的单表继承模型中,我覆盖了 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 问题吗?
最佳答案
答案很简单。只需添加一个 super
到覆盖方法。
def self.inherited(child)
child.instance_eval do
def model_name
BaesModelDefinition.model_name
end
end
super
end
关于ruby-on-rails - 使用 STI 模型名称解决方法的弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728301/