gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 06:34:26 25 4
gpt4 key购买 nike

我有一个直接从 ActiveResource::Base 继承的模型,我正在尝试为记录表中的大部分列运行 alias_method,但结果是一个 NameError:

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

但我可以访问属性:

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

我的类(class)有一个名为 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

关于ruby-on-rails - ActiveRecord::Base 上的 alias_method 导致 NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12309209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com