Forum: Ruby on Rails Multiple associations to the same class

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.
Douglas T. (Guest)
on 2006-02-14 16:13
(Received via mailing list)
Hi,

I cannot seem to create associations like this:

class Project < ActiveRecord::Base
  belongs_to :manager, :class_name => 'User', :foreign_key => 'manager'
  belongs_to :liaison, :class_name => 'User', :foreign_key => 'liaison'
end

p = Project.new
p.manager
=> 1

trying to retrieve associated objects doesn't work and I only receive
the
associated record's ID value as a Fixnum instance. Is there any way to
implement this particular association?
Kent S. (Guest)
on 2006-02-14 20:27
(Received via mailing list)
I guess you should name you association different to the table's field
name.

Kent.
Ryan T. (Guest)
on 2006-02-14 20:36
(Received via mailing list)
On 2/14/06, Kent S. <removed_email_address@domain.invalid> wrote:
> I guess you should name you association different to the table's field name.

Exactly right, that's how I got this sort of setup running. You should
try

  belongs_to :manager_user, :class_name => 'User', :foreign_key =>
'manager'
  belongs_to :liaison_user, :class_name => 'User', :foreign_key =>
'liaison'

or somesuch. Then call project.manager_user when you want the user.

Either that or rename your "manager" column to "manager_id"  in the db.

In my class it's like:

class Bug < ActiveRecord::Base
<snip>
belongs_to :opened_by_user, :class_name => "User", :foreign_key =>
"opened_by"

belongs_to :assigned_to_user, :class_name => "User", :foreign_key =>
"assigned_to"

end

class Bug < ActiveRecord::Base
<snip>
belongs_to :opened_by_user, :class_name => "User", :foreign_key =>
"opened_by"

belongs_to :assigned_to_user, :class_name => "User", :foreign_key =>
"assigned_to"

end
Ryan T. (Guest)
on 2006-02-14 20:39
(Received via mailing list)
sorry I mispasted my User class:

class User < ActiveRecord::Base
<snip>
has_many :bugs_opened, :class_name => "Bug", :foreign_key => "opened_by"

has_many :bugs_assigned, :class_name => "Bug", :foreign_key =>
"assigned_to"
Douglas T. (Guest)
on 2006-02-15 05:19
(Received via mailing list)
hmm, I got it to work by appending '_id' to the :foreign_key values,
like
so: 'manager_id'
AC Green (Guest)
on 2006-02-15 17:58
Douglas T. wrote:
> hmm, I got it to work by appending '_id' to the :foreign_key values,
> like
> so: 'manager_id'

I imagine you don't then need :foreign_key => 'manager_id' I bet RoR
will make that association for you

_T
John N (Guest)
on 2007-03-22 13:28
> I imagine you don't then need :foreign_key => 'manager_id' I bet RoR
> will make that association for you
>
> _T

I wish; it took my hours to find you NEED to define foreign_key if you
use class_name
This topic is locked and can not be replied to.