alias_attribute vs. alias_method for aliasing ActiveRecord attributes
Wolfram Arnold — Tue, 06/16/2009 - 02:29
I've just recently run into a situation where I wanted to add an alias for an accessor method that ActiveRecord creates on the fly.
As you know, if you have a column in your schema, then ActiveRecord will automatically set up the accessor methods, like so:
Now, if you want to access the user's name also by, say, "display_name", then you might ordinarily think you could just:
alias_method :display_name, :name
WRONG! 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.
The way out? Simple, but not widely-known. Rails provides an "alias_attribute" helper:


