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

ActiveRecord :: Base上的ruby-on-rails – alias_method导致Nam

发布时间:2020-12-16 20:29:47 所属栏目:百科 来源:网络整理
导读:我有一个从ActiveResource :: Base继承的模型,我正在为记录表中的大多数列运行alias_method,但结果是一个NameError: NameError: undefined method address_line_1' for class LeadImport::Base’ 但是我可以访问属性: LeadImport::Base.new.address_line_1
我有一个从ActiveResource :: Base继承的模型,我正在为记录表中的大多数列运行alias_method,但结果是一个NameError:

NameError: undefined method address_line_1' for class
LeadImport::Base’

但是我可以访问属性:

LeadImport::Base.new.address_line_1 #=> nil (not error)

我的类有一个名为address_line_1的表列,所以我看不到问题.

class LeadImport::Base < ActiveRecord::Base
    alias_method :address_1,:address_line_1
end

规格:Ruby 1.8.7,Rails 2.3.8

解决方法

根据我发现的一个网站,你应该使用alias_attribute:

The problem is that ActiveRecord doesn’t create the accessor methods
on the fly until the database connection is live and it has parsed the
table schema. That’s a long time after the class has been loaded.

class LeadImport::Base < ActiveRecord::Base
  alias_attribute :address_1,:address_line_1
end

(编辑:李大同)

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

    推荐文章
      热点阅读