• CONTACT

TheTechnologist.TV

  • the column
Home › Blogs › Wolfram Arnold's blog

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:

  • Technology
  • Wolfram Arnold's blog

AUDIT THE FED

Latest

Generative Art From The Math of The Rodin Vortex, The Fibonacci Spiral, and The Gielis Superformula
17 hours 38 min ago
An Historic Press Release
2 weeks 3 days ago
Tahrir Tilt Shift
2 weeks 3 days ago

Below The Fold

On "The Ascent of Money", A BBC Series on The History of Finance
6 weeks 3 days ago
The History of All Earth Ever, as A Drop of Water on The Sun (abstract)
6 weeks 3 days ago
FTC Deals FaceBook The Largest "Hump On The Wrist" In Silicon Valley History
8 weeks 18 hours ago
In The 6 Months Since I Closed The Lab, 11|3L3V3N Happened
10 weeks 2 days ago
When Did I Become Dr Who?
38 weeks 6 days ago
more

Active Topics

  • Technology
  • Google
  • Android
  • iPhone
  • Entertainment

AUDIT THE FED

  • the column