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.
D323e0daf5c222c68f89e0566002d0cc?d=identicon&s=25 Douglas Tan (Guest)
on 2006-02-14 15: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?
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-14 19:27
(Received via mailing list)
I guess you should name you association different to the table's field
name.

Kent.
0d92743c7e40d526a804f2bac4ddb877?d=identicon&s=25 Ryan Tate (Guest)
on 2006-02-14 19:36
(Received via mailing list)
On 2/14/06, Kent Sibilev <ksruby@gmail.com> 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
0d92743c7e40d526a804f2bac4ddb877?d=identicon&s=25 Ryan Tate (Guest)
on 2006-02-14 19: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"
D323e0daf5c222c68f89e0566002d0cc?d=identicon&s=25 Douglas Tan (Guest)
on 2006-02-15 04:19
(Received via mailing list)
hmm, I got it to work by appending '_id' to the :foreign_key values,
like
so: 'manager_id'
F59329dc91cba06600ff65c85fd3e93c?d=identicon&s=25 AC Green (Guest)
on 2006-02-15 16:58
Douglas Tan 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
85e4239ff9662bf8296083d98da17677?d=identicon&s=25 John N (Guest)
on 2007-03-22 12: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.