Associations let us access our data in diverse and flexible ways, simplifying our queries and allow some of our CRUD functionality.
When we are creating apps we start with a map of our initial tables and build them out one at a time.
After we create a table we add any necessary associations. To get some practice, we will start by drawing an ER Diagram, determining the proper associations/relationships between models and test them out in pry.
Note: These are often questions you may get in a technical interview when it comes to modeling a database for an app.
Questions to ask yourself when determining the relationships and validations/restrictions between objects?
- What data do I want to be able to access from these relationships?
- Does "a" have many of "b"? Does "b" have many of "a"?
- Can "a" exist without "b"? Can "b" exist without "a"?
-
Pokemasters
- have a name and gym listed
- can have multiple pokemon
-
Pokemons
- have a name, poketype, and ability
- can be wild or belong to a pokemaster
-
Queries:
- Query a Pokemaster's collection of pokemon
- Query to find out who a pokemon's pokemaster is
- Query to find all wild pokemon
-
Doctors
- Have a first and last name, specialty and location
- Have many patients
-
Patients
- Have a first and last name
- Have many doctors
-
Queries:
- Query a doctor's patients through relationships
- Query a patient's doctors through relationships
-
Appointments
- must have a date, time, and location
- can optionally have a patient listed
- has to have a specific doctor
-
Queries:
- Query a doctor's patients through relationships & appointments
- Query to find out what patient is going to an appointment
- Query to find what appointments are available
- Query to find what doctors have appointments available
-
Professors
- has a first and last name
- has a field of study
- has many students
-
Students
- has a first and last name
- has a expected graduation year
- has many professors
-
Office Hours Appointment
- have to have a date, time
- have to have a professor
- can optionally have a student attend
-
Queries:
- Query a Professor's students
- Query a Student's professors
- Query to find what professor is in charge of a office hour
- Query to find out what student is attending an office hour
- Query to find open slots (without a student)