# Column aliasing in ActiveRecord?

**URL:** <https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006>\
**Category:** Rails\
**Created:** [March 15, 2006, 12:44am UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006 "2006-03-15T00:44:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 15, 2006, 12:44am UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006/1 "2006-03-15T00:44:47Z")

</div>

Anybody know of a way to alias a column in ActiveRecord such that you  
could reference it via a different symbol in your view for view  
purposes?

Like my column is named X but I want to display it as Y and maybe I have  
an accessor named Y set up on the model so that when I “call” Y from the  
view (and controller?) I’m actually manipulating the column X?

Hope that makes sense.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 15, 2006, 2:49am UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006/2 "2006-03-15T02:49:26Z")

</div>

something like this?

def y  
x  
end

def y=(value)  
x = value  
end

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 15, 2006, 7:10am UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006/3 "2006-03-15T07:10:34Z")

</div>

Wes G. wrote:

> Anybody know of a way to alias a column in ActiveRecord such that you  
> could reference it via a different symbol in your view for view purposes?
> 
> Like my column is named X but I want to display it as Y and maybe I have  
> an accessor named Y set up on the model so that when I “call” Y from the  
> view (and controller?) I’m actually manipulating the column X?

Use Ruby’s ‘alias’ feature.

alias :y :x  
alias :y= :x=

Make sure you alias after the :x methods are defined, because a later  
‘def x’ will create a new :x method and :y will point to the old :x.

–josh

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 6, 2006, 11:01am UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006/4 "2006-12-06T11:01:28Z")

</div>

Josh S. wrote:

> Wes G. wrote:
> 
> > Anybody know of a way to alias a column in ActiveRecord such that you  
> > could reference it via a different symbol in your view for view purposes?
> > 
> > Like my column is named X but I want to display it as Y and maybe I have  
> > an accessor named Y set up on the model so that when I “call” Y from the  
> > view (and controller?) I’m actually manipulating the column X?
> 
> Use Ruby’s ‘alias’ feature.
> 
> alias :y :x  
> alias :y= :x=
> 
> Make sure you alias after the :x methods are defined, because a later  
> ‘def x’ will create a new :x method and :y will point to the old :x.
> 
> –josh

this is probably well and all for an experianced programmer to  
understand. But I’m a total noob, so how would you for instance alias a  
column name namned “login” to the alias “foo” in a table namned “users”

Tried something like this… and it does not work:

class User \< ActiveRecord::Base  
include LoginEngine::AuthenticatedUser

belongs\_to :account

```
 def login
  foo
 end

```

def login=(value)  
foo = value  
end

alias :foo :login  
alias :foo= :login=

end

so… how should the define methods look like?

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 6, 2006, 11:15am UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006/5 "2006-12-06T11:15:00Z")

</div>

If you’re trying to bend the login engine plugin into a schema that  
really doesn’t match its assumptions, you’re probably better copying  
the code into your application properly, and then just adjusting it  
and it’s migrations directly - this kind of hacking (while certainly  
possible) is just going to make things more complicated in the long  
run…

FYI, ActiveRecord has an ‘alias\_attribute’ method which does exactly  
what you’re looking for, i.e.

class MyModel \< ActiveRecord::Base  
alias\_attribute :your\_name, :the\_actual\_column\_name  
end

This might only be in Edge Rails though; Caveat Coder.

- James

On 12/6/06, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Use Ruby’s ‘alias’ feature.  
> > understand. But I’m a total noob, so how would you for instance alias a  
> > foo
> 
> so… how should the define methods look like?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 6, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/column-aliasing-in-activerecord/82006/6 "2006-12-06T17:45:24Z")

</div>

Wow, that’s an old original post.

When I need to do display that modifies the original attribute in some  
way on the way in or out, I do something like this:

def zip\_code  
zip = read\_attribute(:Zip)  
zip.nil? ? ‘’ : zip.strip  
end

def zip\_code=(new\_zip)  
write\_attribute(:Zip, new\_zip.strip)  
end

where “Zip” is the column in the table. I don’t use method aliasing to  
accomplish this although I suppose that I could. I tend to like  
explicitly using the read/write attribute functions to remind me of all  
of the layers of attribute management which are involved.

Wes
