Forum: Ruby on Rails How do I make this association?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2007-01-28 18:35
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 %>
B09a3f6cdc4797532647d2d264b5df49?d=identicon&s=25 Jodi Showers (jshow)
on 2007-01-28 18:41
(Received via mailing list)
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
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2007-01-28 19:14
(Received via mailing list)
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...
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2007-01-28 20:15
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 Susser
http://blog.hasmanythrough.com/
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2007-01-28 20:39
(Received via mailing list)
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 Susser <rails-mailing-l...@andreas-s.net>
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-28 21:12
(Received via mailing list)
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)
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2007-01-28 21:56
(Received via mailing list)
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.
This topic is locked and can not be replied to.