加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用STI model_name解决方法的弃用警告

发布时间:2020-12-17 02:36:04 所属栏目:百科 来源:网络整理
导读:在我的单表继承模型中,我重写了基本模型中的继承方法,以便所有后代模型都被基本模型的名称识别.以下代码用于为所有继承的类添加对model_name方法的覆盖. def self.inherited(child) child.instance_eval do def model_name BaesModelDefinition.model_name e
在我的单表继承模型中,我重写了基本模型中的继承方法,以便所有后代模型都被基本模型的名称识别.以下代码用于为所有继承的类添加对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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读