DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Rails Aliasing Of Database Column Names
Add to your model:
GreenPastures < ActiveRecord::Base
alias_column "new_name" => "crappy_old_nAmE"
Include this code in a file in /lib
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






Comments
Snippets Manager replied on Mon, 2012/05/07 - 2:13pm
alias new_name old_nameseems to be the opposite ofalias_column 'old_name' => 'new_name'Maybe reverse it?