Forum: Ruby on Rails has_and_belongs_to_many linking more than two tables

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.
C108514e40e8ed717aa3e997524c8c67?d=identicon&s=25 geoffeg (Guest)
on 2007-06-20 20:58
(Received via mailing list)
I have a database with a central lookup table that links about 5
tables together (the examples below are theoretical, I made them up to
illustrate my point):

CREATE TABLE lookup (patient_id int,doctor_id int,nurse_id  int);
CREATE TABLE patients (id int, name varchar2(200));
CREATE TABLE doctors (id int, name varchar2(200));
CREATE TABLE nurses (id int, name varchar2(200));

I can throw a has_and_belongs_to_many :patients in doctors.rb and a
has_and_belongs_to_many :doctors in patients.rb but how do I get
nurses in there?

The lookup table I'm using actually has more than three target tables
that it joins. There's 5 tables. The ActiveRecord docs say "Associates
two classes via an intermediate join table." for
has_and_belongs_to_many. How do I associate 5 classes via an
intermediate join table?

Thanks,
Geoffeg
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-06-21 07:36
(Received via mailing list)
haven't actually tried it, but how about

Doctor
 has_and_belongs_to_many :nurses, :join_table => 'lookup'
 has_and_belongs_to_many :patients, :join_table => 'lookup'

Nurse
 has_and_belongs_to_many :doctors, :join_table => 'lookup'
 has_and_belongs_to_many :patients, :join_table => 'lookup'

Patient
 has_and_belongs_to_many :doctors, :join_table => 'lookup'
 has_and_belongs_to_many :nurses, :join_table => 'lookup'
This topic is locked and can not be replied to.