10. MVC Model Review
What DB table would go with this model class?
first_name : string
last_name : string
year : integer
Author
Model Class: Database Table:
???
11. MVC Model Review
What DB table would go with this model class?
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name : string
last_name : string
year : integer
Author
Model Class: Database Table:
12. MVC Model Review
What DB table would go with this model class?
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name : string
last_name : string
year : integer
Author
Model Class: Database Table:
Recall Rails ORM
(Object-Relational Mapping)
13. MVC Model Review
What Author methods does Rails
provide “for free”?
first_name : string
last_name : string
year : integer
Author
Model Class:
14. MVC Model Review
What Author methods does Rails
provide “for free”?
first_name : string
last_name : string
year : integer
Author
Model Class: Many methods:
• Author.create / Author.create!
• Author.find
• Author.find_by
• Author.all
• Getters/Setters for attributes:
⎼ Author#first_name
⎼ Author#first_name=
• Author#save / Author#save!
• Author#destroy
• Author#valid?
15. id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name : string
last_name : string
year : integer
Author
Model Class:
Limitation so far:
Insular model classes/tables
Database Table:
16. id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
first_name : string
last_name : string
year : integer
Author
Model Class:
Imagine an author-profile class
Database Table:
birthplace : string
bio : text
awards : text
AuthorProfile
id birthplace bio awards
23 Saint Petersburg Rand was born… Prometheus Award, …
42 New York City He was the son of… Shark Conservation, …
author_profiles
17. What if you want inter-class relationships?
first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
???
For example, you might want to say:
19. Rails Model Supports Three Associations
A B
1
has ▶︎ one to one
(has one / belongs to one)
A B
*
has ▶︎ one to many
(has many / belongs to one)
A B
* many to many
(join table)
1
1
*
AB
20. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
21. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Class Association
22. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Association Name
Triangle indicates reading direction
(i.e., Author has AuthorProfile)
23. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Role Names
Like attribute/method name
Enables this method call:
@author.profile.birthplace
24. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Multiplicities
25. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Multiplicities
Each Author has 1 AuthorProfile
26. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Multiplicities
Each Author has 1 AuthorProfile
Each AuthorProfile belongs to 1 Author
27. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Multiplicities
Each Author has 1 AuthorProfile
Each AuthorProfile belongs to 1 Author
Zero-or-More Multiplicities:
A B
*
has
Each A has 0 or more Bs
A B
0..*
has
Each A has 0 or more Bs
28. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
first_name = "Ayn"
last_name = "Rand"
year_born = 1905
: Author
first_name = "Peter"
last_name = "Benchley"
year_born = 1940
: Author
birthplace = "Saint Petersburg"
bio = "Rand was born..."
awards = "Prometheus Award, …"
: AuthorProfile
birthplace = "New York City"
bio = "He was the son of..."
awards = " Shark Conservation, … "
: AuthorProfile
Associations manifest as links in object diagrams
author profile
author profile
29. Class Association Notation
name : string
Physician
name : string
Patient
*
physician
*
patient
appointment_date : datetime
Appointment
Association Class
Each instance is an association link
Can add additional data to each link (e.g., date)
30. name : string
Physician
name : string
Patient
*
physician
*
patient
appointment_date : datetime
Appointment
Enables code like this:
Class Association Notation
31. name : string
Physician
name : string
Patient
*
physician
*
patient
appointment_date : datetime
Appointment
Class Association Notation
name = "Dr. Hibbert"
: Physician
name = "Homer Simpson"
: Patient
name = "Marge Simpson"
: Patient
appointment_date = ...
: Appointment
appointment_date = ...
: Appointment
physician
patient
physician
patient
How many-to-many links look in object diagrams:
33. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
To map this association to relational DB...
first_name = "Ayn"
last_name = "Rand"
year_born = 1905
: Author
first_name = "Peter"
last_name = "Benchley"
year_born = 1940
: Author
birthplace = "Saint Petersburg"
bio = "Rand was born..."
awards = "Prometheus Award, …"
: AuthorProfile
birthplace = "New York City"
bio = "He was the son of..."
awards = " Shark Conservation, … "
: AuthorProfile
author profile
author profile
34. id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id birthplace bio awards
23 Saint Petersburg Rand was born… Prometheus Award, …
42 New York City He was the son of… Shark Conservation, …
author_profiles
Requires inter-table references
35. Rails ORM Varies by Type of Association
A B
1
has ▶︎ one to one
(has one / belongs to one)
A B
*
has ▶︎ one to many
(has many / belongs to one)
A B
* many to many
(join table)
1
1
*
AB
36. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Has One / Belongs To One Example
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id author _id birthplace bio awards
23 1 Saint Petersburg Rand was born… Prometheus Award, …
42 2 New York City He was the son of… Shark Conservation, …
author_profiles
Rails ORM DB Tables
37. first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Has One / Belongs To One Example
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id author _id birthplace bio awards
23 1 Saint Petersburg Rand was born… Prometheus Award, …
42 2 New York City He was the son of… Shark Conservation, …
author_profiles
Rails ORM DB Tables
38. id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id author _id birthplace bio awards
23 1 Saint Petersburg Rand was born… Prometheus Award, …
42 2 New York City He was the son of… Shark Conservation, …
author_profiles
Primary versus Foreign Keys
Primary Key: Uniquely identifies each record in table
39. id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id author _id birthplace bio awards
23 1 Saint Petersburg Rand was born… Prometheus Award, …
42 2 New York City He was the son of… Shark Conservation, …
author_profiles
Primary versus Foreign Keys
Primary Key: Uniquely identifies each record in table
Foreign Key: Reference to primary key in another table
40. id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id author _id birthplace bio awards
23 1 Saint Petersburg Rand was born… Prometheus Award, …
42 2 New York City He was the son of… Shark Conservation, …
author_profiles
Primary versus Foreign Keys
Primary Key: Uniquely identifies each record in table
Foreign Key: Reference to primary key in another table
41. How to Set Up Association in Rails
1. Generate migration that adds reference (foreign
key) column to appropriate table
2. Add has_one/belongs_to declarations to
model class
See forthcoming demo for details
43. Has Many / Belongs to One Example
first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
title : string
year : integer
summary : text
Book
How to model authorship?
(assume 1 author per book)
44. Has Many / Belongs to One Example
first_name : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
title : string
year : integer
summary : text
Book
How to model authorship?
(assume 1 author per book)
has
1 author
* book
45. Has Many / Belongs to One Example
first_name : string
last_name : string
year : integer
Author
title : string
year : integer
summary : text
Book
What should the ORM be?
(hint: authors table unchanged)
has
1 author
* book
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
46. Has Many / Belongs to One Example
first_name : string
last_name : string
year : integer
Author
title : string
year : integer
summary : text
Book
What should the ORM be?
(hint: authors table unchanged)
has
1 author
* book
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id title year summary author_id
72 The Fountainhead 1943 Individualistic architect… 1
98 Atlas Shrugged 1957 Dystopian USA… 1
99 Jaws 1974 Shark!... 2
books
47. Has Many / Belongs to One Example
first_name : string
last_name : string
year : integer
Author
title : string
year : integer
summary : text
Book
What should the ORM be?
(hint: authors table unchanged)
has
1 author
* book
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id title year summary author_id
72 The Fountainhead 1943 Individualistic architect… 1
98 Atlas Shrugged 1957 Dystopian USA… 1
99 Jaws 1974 Shark!... 2
books
48. Has Many / Belongs to One Example
first_name : string
last_name : string
year : integer
Author
title : string
year : integer
summary : text
Book
has
1 author
* book
id first_name last_name year
1 Ayn Rand 1905
2 Peter Benchley 1940
authors
id title year summary author_id
72 The Fountainhead 1943 Individualistic architect… 1
98 Atlas Shrugged 1957 Dystopian USA… 1
99 Jaws 1974 Shark!... 2
books
Rails generates books method
that returns list of books
49. How to Set Up in Rails
• Essentially the same as has_one
• Again, see the demo
55. Join Table Example: Model Class
through enables this sort of thing:
From http://guides.rubyonrails.org/v4.2.0/association_basics.html
56. How to Set Up in Rails
• See the Model Association demos
• For more info:
– http://guides.rubyonrails.org/v6.1.0/association_basics.html
– http://api.rubyonrails.org/v6.1.0/classes/ActiveRecord/Asso
ciations/ClassMethods.html