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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...