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