# How do I make this association?

**URL:** <https://www.ruby-forum.com/t/how-do-i-make-this-association/83673>\
**Category:** Rails\
**Created:** [January 28, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673 "2007-01-28T18:35:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/1 "2007-01-28T18:35:59Z")

</div>

I’ve got an association question, involving a Ticket model and a User  
model. My ticket model has integer fields, among others, called  
opened\_by and created\_by, linking to user id’s. So basically, how do I  
make that association so I can access the user fields in my ticket  
views? Thanks in advance for any help!

This is what I have so far:  
Ticket belongs\_to :user, :foreign\_key =\> “created\_by”  
Ticket belongs\_to :user, :foreign\_key =\> “opened\_by”  
User has\_many :tickets

In my view, I want to do this:  
\<%=h @ticket.opened\_by.username %\>

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/2 "2007-01-28T18:41:19Z")

</div>

On 28-Jan-07, at 12:35 PM, Jl Smith wrote:

> Ticket belongs\_to :user, :foreign\_key =\> “opened\_by”  
> User has\_many :tickets
> 
> In my view, I want to do this:  
> \<%=h @ticket.opened\_by.username %\>

Hey JL,

Use Delynn’s Userstamp plugin described here:

[http://delynnberry.com/projects/userstamp/](http://delynnberry.com/projects/userstamp/)

Cheers,  
Jodi  
General Partner  
The nNovation Group inc.  
www.nnovation.ca/blog

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/3 "2007-01-28T19:14:07Z")

</div>

Thanks for the link Jodi. I wanted to handle the business logic of  
updating those fields myself. I’m just not seeing the association  
though…

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/4 "2007-01-28T20:15:07Z")

</div>

Jl Smith wrote:

> I’ve got an association question, involving a Ticket model and a User  
> model. My ticket model has integer fields, among others, called  
> opened\_by and created\_by, linking to user id’s. So basically, how do I  
> make that association so I can access the user fields in my ticket  
> views? Thanks in advance for any help!
> 
> This is what I have so far:  
> Ticket belongs\_to :user, :foreign\_key =\> “created\_by”  
> Ticket belongs\_to :user, :foreign\_key =\> “opened\_by”  
> User has\_many :tickets
> 
> In my view, I want to do this:  
> \<%=h @ticket.opened\_by.username %\>

You are using the name :user twice to create associations. That is the  
name of the associations, and must be unique within the class. Also, you  
need to differentiate the kinds of tickets a user may have. You want to  
do something like this:

class Ticket \< ActiveRecord::Base  
belongs\_to :creator, :class\_name =\> “User”, :foreign\_key =\>  
“creator\_id”  
belongs\_to :opener, :class\_name =\> “User”, :foreign\_key =\> “opener\_id”  
end

class User \< ActiveRecord::Base  
has\_many :created\_tickets, :class\_name =\> “Ticket”, :foreign\_key =\>  
“creator\_id”  
has\_many :opened\_tickets, :class\_name =\> “Ticket”, :foreign\_key =\>  
“opener\_id”  
end

I changed the foreign key names to follow the Rails convention of using  
\_id. In Rails 2.0, the belongs\_to method will change so that the  
association name will imply the name of the foreign key instead of the  
name of the class, so this will set you up for that eventual change.

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/5 "2007-01-28T21:12:38Z")

</div>

Hi –

On Sun, 28 Jan 2007, JL Smith wrote:

> Ticket:  
> belongs\_to :creator, :class\_name =\> “User”, :foreign\_key =\>  
> “creator\_id”  
> belongs\_to :opener, :class\_name =\> “User”, :foreign\_key =\> “opener\_id”
> 
> In my view I’m still doing this:  
> \<%=h @ticket.created\_by.username %\>
> 
> But I’m still doing something wrong (undefined method error). Should  
> I just go ahead and change the foreign key names in the tickets table  
> to creator\_id and opener\_id? Thanks the help!

I think it’s just that Josh used creator and opener instead of  
created\_by and opened\_by as the association names. Try:

@ticket.creator.username

Assuming that’s OK, you can switch around as desired. Just keep them  
the same (i.e., the association name and the name of the method you  
call on @ticket).

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/6 "2007-01-28T21:56:45Z")

</div>

Awesome. Thanks David. I think I’ve got the associations working  
now.

But I’ve also got one small hiccup from all this. My business logic  
is such that any new ticket will have a created\_by and created\_at  
field, but not necessarily an opened\_at and opened\_by field. So when  
I come to my ticket view after creating a new ticket and it hits \<%=h  
@ticket.opener.username %\>, I get the “You have a nil object when you  
didn’t expect it!” error. Of course I don’t get this error when the  
ticket has an opened\_by value. So how do I avoid this? Am I supposed  
to check for nil before I try to display it? I’ve never had to do  
that before…is it because of this new association I’ve created?  
Thanks again.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [January 28, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/how-do-i-make-this-association/83673/7 "2007-01-28T20:39:32Z")

</div>

Ok, so to get this to work, I’ll need to keep my foreign key names in  
the tickets table until I change them in my migration later. So I  
have this:

User:  
has\_many :created\_tickets, :class\_name =\> “Ticket”, :foreign\_key =\>  
“created\_by”  
has\_many :opened\_tickets, :class\_name =\> “Ticket”, :foreign\_key =\>  
“opened\_by”

Ticket:  
belongs\_to :creator, :class\_name =\> “User”, :foreign\_key =\>  
“creator\_id”  
belongs\_to :opener, :class\_name =\> “User”, :foreign\_key =\> “opener\_id”

In my view I’m still doing this:  
\<%=h @ticket.created\_by.username %\>

But I’m still doing something wrong (undefined method error). Should  
I just go ahead and change the foreign key names in the tickets table  
to creator\_id and opener\_id? Thanks the help!

On Jan 28, 1:15 pm, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
