Forum: Ruby on Rails Two "has_one" relationships linking to the same table?

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.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris Richards (chris)
on 2005-11-19 19:47
How do i get two "has_one" relationships to the same table?

I have an audit table with fields auditor_id and manager_id that both
link to the people table:

AUDIT:
id
name
datecreated
auditor_id
manager_id

PEOPLE:
id
name

I have no idea how to link both fields from audit to people?? any
ideas??
I'm pulling my hair out trying to figure this out, please help!!

I know that if it were just one field then i'd call the field person_id.
What do i call a second field linking to the same table ???
person_id_2???
please help.


Thanks,
Chris
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 petermichaux (Guest)
on 2005-11-19 22:44
(Received via mailing list)
Hi,

I think the trick is getting the has_many and belong_to in the correct
places. Since Audit references the People class, it is Audit that
belongs to
the People. I hope I got this right way around. I think this should work
(untested code).

Class Audit < ActiveRecord::Base

belongs_to :auditor,
:class_name => "People",
:foreign_key => "auditor_id"

belongs_to :manager,
:class_name => "People",
:foreign_key => "manager_id"

end

Class People < ActiveRecord::Base

has_many :auditor_audits,
:class_name => "Audit",
:foreign_key => "auditor_id"

has_many :manager_audits,
:class_name => "Audit",
:foreign_key => "manger_id"

end


belongs_to docs:
http://api.rubyonrails.com/classes/ActiveRecord/As...

has_many documentation:
http://api.rubyonrails.com/classes/ActiveRecord/As...

-Peter
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-11-20 05:08
(Received via mailing list)
You might want to look into single table inheritance...

example:

people
----------
id
name
type

audits
----------
id
auditor_id
manager_id

class Person < ActiveRecord::Base
end

class Auditor < Person
has_many :audits
# auditor specific functionality
end

class Manager < Person
has_many :audits
#manager specific functionality
end

class Audit < ActiveRecord
belongs_to :auditor
belongs_to :manager
end

bob = Manager.create(:name => "Bob")
joe = Auditor.create(:name => "Joe")
Audit.create(:manager_id => bob.id <http://bob.id>, :auditor_id =>
joe.id<http://joe.id>
)
...
audit = Audit.find(1)
puts audit.manager # ==> bob
puts audit.auditor # ==> joe
...
puts bob.audits
puts joe.audits
1cbcd04f1aa12775ce0f51d847bb4849?d=identicon&s=25 Lee Iverson (leei)
on 2005-11-21 19:37
(Received via mailing list)
Chris Hall wrote:

> audits
>   # auditor specific functionality
> end
>
> class Manager < Person
>   has_many :audits
>   #manager specific functionality
> end
>
Not going to cut it.  This logically implies that a person can be an
Auditor or a Manager but not both and identifes them in the Person
record (by the mandatory 'type' field for STI.

Why not just:

class Audit < ActiveRecord::Base
  has_one :auditor, :foreign_key => 'auditor_id', :class_name =>
'Person'
  has_one :manager, :foreign_key => 'manager_id', :class_name =>
'Person'
end
This topic is locked and can not be replied to.