Never been to DZone Snippets before?

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 (See related posts)


Add to your model:

GreenPastures < ActiveRecord::Base

alias_column "new_name" => "crappy_old_nAmE"

Include this code in a file in /lib

   1  
   2  module Legacy
   3    def self.append_features(base)
   4      super
   5      base.extend(ClassMethods)
   6    end
   7    module ClassMethods
   8      def alias_column(options)
   9        options.each do |new_name, old_name|
  10          self.send(:define_method, new_name) { self.send(old_name) }
  11          self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) }
  12        end
  13      end
  14    end
  15  end
  16  
  17  ActiveRecord::Base.class_eval do
  18    include Legacy
  19  end

Comments on this post

canadaduane posts on Aug 05, 2005 at 21:05
This is a fantastic, simple add-on. I wonder, however, if the aliasing is kind of backwards compared to the Ruby 'alias' keyword?

   1  
   2  alias new_name old_name


seems to be the opposite of

   1  
   2  alias_column 'old_name' => 'new_name'


Maybe reverse it?

You need to create an account or log in to post comments to this site.


Click here to browse all 5829 code snippets

Related Posts