- Write classes that are related to each other via the "has-many-through" relationship.
- Write methods that use the "has-many-through" relationship to deliver information on related objects.
In this lab, we'll be dealing with two sets of three classes.
We have a Song
, Artist
and Genre
class. Songs belong to an artist and
belong to a genre. A genre has many songs and an artist has many songs. An
artist has many genres through its songs and a genre has many artists through
its songs.
We also have an Appointment
, Doctor
and Patient
class. Appointment
s
belong to a Patient
and a Doctor
. A Doctor
has many Appointment
s and a
Patient
has many Appointment
s. A Doctor
has many Patient
s through its
Appointment
s and a Patient
has many Doctor
s through its Appointment
s.
Start with the Artist
/Song
/Genre
domain.
The Artist
model:
The Song
model:
The Genre
model:
Now let's move on to our Doctor
/Appointment
/Patient
domain model.
The Doctor
model:
- The
Doctor
class needs an instance method,#new_appointment
, that takes in a an instance of thePatient
class and a date, and creates a newAppointment
. ThatAppointment
should know that it belongs to the doctor - The
Doctor
class needs an instance method,#appointments
, that iterates through allAppointment
s and finds those belonging to this doctor. - The
Doctor
class needs an instance method,#patients
, that iterates over that doctor'sAppointment
s and collects the patient that belongs to eachAppointment
s.
The Appointment
model:
The Patient
model:
- The
Patient
class needs an instance method,#new_appointment
, that takes in an argument of a doctor and a date and creates a newAppointment
. TheAppointment
should know that it belongs to the patient. - The
Patient
class needs an instance method,#appointments
, that iterates through theAppointment
s array and returnsAppointment
s that belong to the patient. - The
Patient
class needs an instance method,#doctors
, that iterates over that patient'sAppointment
s and collects the doctor that belongs to eachAppointment
.
View Has Many Objects Through Lab on Learn.co and start learning to code for free.