# Aliasing Active Record Attributes (also delegation issues)

**URL:** <https://www.ruby-forum.com/t/aliasing-active-record-attributes-also-delegation-issues/82780>\
**Category:** Rails\
**Created:** [January 20, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/aliasing-active-record-attributes-also-delegation-issues/82780 "2007-01-20T21:06:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 20, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/aliasing-active-record-attributes-also-delegation-issues/82780/1 "2007-01-20T21:06:25Z")

</div>

This whole situation is a mess, but I’ll see if I can explain it well.

I have two models, User and Company. I’d like user to be able to get  
company.name. I’m aware of the problems with using @user.company.name,  
because of company is nil, then name throws an error.

So originally, I wrote this method instead

class User  
def company\_name  
company ? company.name : nil  
end  
end

This works, however, rather than use that, I would like to use  
delegation.  
The problem I ran into with that,  
however, is that user also has a name. So I can’t simply write

class User  
delegate :name, :to =\> :company  
end

because that will overwrite name. It would be nice if there was a  
delegate  
:company\_name, :as =\> :name, :to =\> :company, but as far as I know this  
doesn’t exist. So… I thought I would use “delegate :company\_name: to  
=\>  
:company”, and alias :company\_name to :name within company, like so:

class Company  
alias :company\_name :name  
end

however, that will not work with Active Record models, because name is  
not  
defined until the class is instantiated. I could just write a wrapper  
method, aka def class\_name; name; end; but that adds an extra method  
call  
that I’d rather not have. So finally, I came up with this solution:

class Company  
def company\_name  
name  
self.class.class\_eval do  
alias\_method :company\_name, :name  
end  
name  
end  
end

This works, because the first time company\_name is called, it calls name  
to  
instantiate it (needed the first call for some reason), then it opens  
the  
class and aliases company\_name to name. Lastly, it returns name, so  
that it  
works as intended on the first call as well.  
This code does work, I have tested it, but it seems like an awful lot of  
work for what seems like a simple concept. Maybe I’m just making things  
too  
complicated, and should have simply used my company\_name method within  
User,  
I don’t know.

If anyone can suggest a better way to solve this problem, it would be  
much  
appreciated.

Thanks,  
Tyler P.

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 21, 2007, 5:03am UTC](https://www.ruby-forum.com/t/aliasing-active-record-attributes-also-delegation-issues/82780/2 "2007-01-21T05:03:08Z")

</div>

I’m not entirely sure, but i think @object.content.name only trows an  
error if @object.content is nil AND you’re in development mode (whiny  
nils is enabled). Not sure though.

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 21, 2007, 10:49am UTC](https://www.ruby-forum.com/t/aliasing-active-record-attributes-also-delegation-issues/82780/3 "2007-01-21T10:49:37Z")

</div>

On 1/20/07, Tyler P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Company  
> alias :company\_name :name  
> end

class Company  
alias\_attribute :company\_name, :name  
end

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 21, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/aliasing-active-record-attributes-also-delegation-issues/82780/4 "2007-01-21T15:46:08Z")

</div>

Chris,

Thanks! Much much better. I had the feeling that there had to be a  
better  
way, and there it is.

–Tyler

P.S. I really like your blog. Lots of good information on there.
