2012-09-06 36 views
14

的ActiveRecord :: Base的結果我有一個模型直接從ActiveResource::Base繼承,而我試圖運行alias_method大多數在記錄的表中的列,但結果是一個NameError: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 

規格:紅寶石1.8.7,2.3.8的Rails

回答

23

據一個網站,我發現,你應該使用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 
+0

我更新我的答案與應該工作的東西:) –