Alias for column names in Rails

activerecord, database, ruby-on-rails, ruby-on-rails-3

Solution

Declare this in your model.

alias_attribute :new_column_name, :column_name_in_db

Problem

In my database has column names such as 'delete' or 'listen-control' and so on. These cannot be changed, so I would like to alias the names so as to avoid problems in my application. I found the following code but it is outdated (05 Aug 2005) and doesn't work with Rails 3: ``` module Legacy def self.append_features(base) super base.extend(ClassMethods) end module ClassMethods def alias_column(options) options.each do |new_name, old_name| self.send(:define_method, new_name) { self.send(old_name) } self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) } end end end end ActiveRecord::Base.class_eval do include Legacy end ``` How can I alias the column names? Is it possible?

Original source