微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个从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

原文地址:https://www.jb51.cc/ruby/273502.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐