How do I make this association?


#1

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 %>


#2

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/

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


#3

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…


#4

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/


#5

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 (http://www.manning.com/black)
(See what readers are saying! 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)


#6

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.


#7

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