- Construct a bi-directional has many through.
- Query for associations via the belongs_to, has_many, and has_many through associations.
- Iterate over associations in a view and display associated data for a primary instance.
- Identify the join model in a has many through.
The poor doctors at Seattle Grace Hospital are so overworked that they're having trouble keeping track of their patients' appointments! Let's help them (and practice our associations) by building some associations and displaying the data.
First, fork and clone this lab.
You'll need to create all of the migrations, models, routes, controllers, and views for this lab.
NOTE: As with much of our Rails curriculum, remember to always use the --no-test-framework
flag when you generate models, controllers, etc. That way, the Rails generators will not create additional tests on top of the test suite that already comes with the lesson. E.g., rails g model User username:string email:string --no-test-framework
.
-
Create migrations and models for doctor, patient, and appointment. Because this is for a hospital, a doctor can have many patients, but a patient can also have many doctors. Patients and doctors both have many appointments. Our
appointments
table should have a column calledappointment_datetime
to represent the date and time the appointment will take place. (When you display this datetime, make sure it's in a human readable format. The tests will be expecting "January 12, 2016 at 3:00".) -
Create a
Doctor#index
page that displays each doctor's name, a link to their show page, and a count of their patients. -
Create a
Doctor#show
page that displays the doctor's name, department, and appointments, with each appointment's date, time, and patient name (linking to the patient's show page). -
Create a
Patient#show
page that lists the date and time for each of their appointments and links to the corresponding doctor's show page. -
Create a
Patient#index
page that displays a link to each patient's show page and the total number of appointments they have. -
Create an
Appointment#show
page that shows the date, time, patient, and doctor for that particular appointment. Note that there should NOT be anAppointment#index
page –– we don't want to have all of the appointments on display for anyone to see.
We've provided a seed file so you can have some data to play around with –– run rake db:seed
once your migrations and models are complete.
View Displaying Has Many Through Rails Lab on Learn.co and start learning to code for free.