ActiveRecord::Base 上的 alias_method 导致 NameError [英] alias_method on ActiveRecord::Base results in NameError

查看:29
本文介绍了ActiveRecord::Base 上的 alias_method 导致 NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个直接从 ActiveResource::Base 继承的模型,我正在尝试为记录表中的大多数列运行 alias_method,但结果是一个名称错误:

I have a model inheriting directly from ActiveResource::Base, and I'm trying to run alias_method for most of the columns in the record's table, but the result is a NameError:

NameError: undefined method address_line_1' for classLeadImport::Base'

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

但我可以访问该属性:

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

我的班级有一个名为 address_line_1 的表列,所以我看不出问题.

My class has a table column named address_line_1, so I fail to see the problem.

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

规格:Ruby 1.8.7,Rails 2.3.8

推荐答案

根据我找到的一个站点,您应该使用 alias_attribute 代替:

According a site I found, you're supposed to use alias_attribute instead:

问题是 ActiveRecord 没有创建访问器方法动态直到数据库连接处于活动状态并且它已经解析了表架构.这是类加载后的很长时间.

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

这篇关于ActiveRecord::Base 上的 alias_method 导致 NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆