17

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 class LeadImport::Base'

Yet I can access the attribute:

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

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

specs: Ruby 1.8.7, Rails 2.3.8

JellicleCat
  • 28,480
  • 24
  • 109
  • 162

1 Answers1

28

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

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
Peter Brown
  • 50,956
  • 18
  • 113
  • 146