SlideShare une entreprise Scribd logo
1  sur  57
MVC Model Associations
https://flic.kr/p/dxGXD
Tests
Migrations
Ye Olde
Internet
Model
DB
Server
Router
View
Browser
Controller
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
Tests
Migrations
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
MVC Model Review
What key capabilities do Rails
model classes provide?
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
MVC Model Review
What key capability do Rails
model classes provide?
CRUD persistent data
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
MVC Model Review
How do you create Rails
model classes?
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
MVC Model Review
How do you create Rails
model classes?
Generate DB migrations and
model classes with
“rails g model …”
then customize
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
MVC Model Review
What purpose do migrations
serve?
Ye Olde
Internet
Server
Router Controller
View
Browser
Model
DB
MVC Model Review
What purpose do migrations
serve?
Setup tables in DB via, e.g.,
“rails db:migrate” command
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:
???
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:
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)
MVC Model Review
What Author methods does Rails
provide “for free”?
first_name : string
last_name : string
year : integer
Author
Model Class:
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?
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:
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
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:
Rails Model Associations
Topics:
• OO Class Associations
• Rails ORM
• How to code in Rails
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
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 : string
last_name : string
year : integer
Author
birthplace : string
bio : text
awards : text
AuthorProfile
has
1
author
1
profile
Class Association Notation
Class Association
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)
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
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
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
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
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
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
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)
name : string
Physician
name : string
Patient
*
physician
*
patient
appointment_date : datetime
Appointment
Enables code like this:
Class Association Notation
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:
Rails Model Associations
Topics:
• OO Class Associations
• Rails ORM
• How to code in Rails
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
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
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
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
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
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
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
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
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
Rails Relationship Support
• has one / belongs to one
• has many / belongs to one
• join table
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 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
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
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
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
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
How to Set Up in Rails
• Essentially the same as has_one
• Again, see the demo
Rails Relationship Support
• has one / belongs to one
• has many / belongs to one
• join table
Example Many-to-Many Association
name : string
Physician
name : string
Patient
*
physician
*
patient
appointment_date : datetime
Appointment
Join Table Example
From http://guides.rubyonrails.org/v4.2.0/association_basics.html
Join Table Example
Join table
From http://guides.rubyonrails.org/v4.2.0/association_basics.html
Join Table Example
References
(aka foreign keys)
From http://guides.rubyonrails.org/v4.2.0/association_basics.html
Join Table Example: Model Class
through enables this sort of thing:
From http://guides.rubyonrails.org/v4.2.0/association_basics.html
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
Summary
• Model associations
• Foreign keys
• Rails ORM for assoc.
• Has-one/belong-to-one
• Has-many/belongs-to-one
• Many-to-many join table
http://flic.kr/p/aCLor3

Contenu connexe

Similaire à associations.pptx

SEE Formatting Template 11 pt. fontno word limitChapter.docx
SEE Formatting Template 11 pt. fontno word limitChapter.docxSEE Formatting Template 11 pt. fontno word limitChapter.docx
SEE Formatting Template 11 pt. fontno word limitChapter.docxjeffreye3
 
Semantic MediaWiki Workshop
Semantic MediaWiki WorkshopSemantic MediaWiki Workshop
Semantic MediaWiki WorkshopDan Bolser
 
Refreshing your senses with mla
Refreshing your senses with mlaRefreshing your senses with mla
Refreshing your senses with mlaKathe Santillo
 
Refreshing your senses with MLA
Refreshing your senses with MLARefreshing your senses with MLA
Refreshing your senses with MLAcrmcfeely
 
Complete Guide to MLA 8 by easyBib
Complete Guide to MLA 8 by easyBibComplete Guide to MLA 8 by easyBib
Complete Guide to MLA 8 by easyBibJonathan Underwood
 
LIS 653, Session 4-A: Bibliographic Formats and MARC
LIS 653, Session 4-A: Bibliographic Formats and MARC LIS 653, Session 4-A: Bibliographic Formats and MARC
LIS 653, Session 4-A: Bibliographic Formats and MARC Dr. Starr Hoffman
 
SWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mappingSWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mappingMariano Rodriguez-Muro
 
Mon norton tut_publishing01
Mon norton tut_publishing01Mon norton tut_publishing01
Mon norton tut_publishing01eswcsummerschool
 
Karen Coyle: New Models of Matadata
Karen Coyle: New Models of MatadataKaren Coyle: New Models of Matadata
Karen Coyle: New Models of MatadataALATechSource
 
Rda-What we need to know
Rda-What we need to knowRda-What we need to know
Rda-What we need to knowccase2
 
LIS 653, Session 6: FRBR & Relationships
LIS 653, Session 6: FRBR & Relationships LIS 653, Session 6: FRBR & Relationships
LIS 653, Session 6: FRBR & Relationships Dr. Starr Hoffman
 
¿ARCHIVO?
¿ARCHIVO?¿ARCHIVO?
¿ARCHIVO?ESPOL
 
que hisciste el verano pasado
que hisciste el verano pasadoque hisciste el verano pasado
que hisciste el verano pasadoespol
 

Similaire à associations.pptx (17)

SEE Formatting Template 11 pt. fontno word limitChapter.docx
SEE Formatting Template 11 pt. fontno word limitChapter.docxSEE Formatting Template 11 pt. fontno word limitChapter.docx
SEE Formatting Template 11 pt. fontno word limitChapter.docx
 
Semantic MediaWiki Workshop
Semantic MediaWiki WorkshopSemantic MediaWiki Workshop
Semantic MediaWiki Workshop
 
Refreshing your senses with mla
Refreshing your senses with mlaRefreshing your senses with mla
Refreshing your senses with mla
 
Refreshing your senses with MLA
Refreshing your senses with MLARefreshing your senses with MLA
Refreshing your senses with MLA
 
Complete guide MNL
Complete guide MNLComplete guide MNL
Complete guide MNL
 
Complete Guide to MLA 8 by easyBib
Complete Guide to MLA 8 by easyBibComplete Guide to MLA 8 by easyBib
Complete Guide to MLA 8 by easyBib
 
LIS 653, Session 4-A: Bibliographic Formats and MARC
LIS 653, Session 4-A: Bibliographic Formats and MARC LIS 653, Session 4-A: Bibliographic Formats and MARC
LIS 653, Session 4-A: Bibliographic Formats and MARC
 
MLA Citation Guide
MLA Citation GuideMLA Citation Guide
MLA Citation Guide
 
SWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mappingSWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mapping
 
Mon norton tut_publishing01
Mon norton tut_publishing01Mon norton tut_publishing01
Mon norton tut_publishing01
 
RDA: an introduction
RDA: an introductionRDA: an introduction
RDA: an introduction
 
Karen Coyle: New Models of Matadata
Karen Coyle: New Models of MatadataKaren Coyle: New Models of Matadata
Karen Coyle: New Models of Matadata
 
Elixir + Neo4j
Elixir + Neo4jElixir + Neo4j
Elixir + Neo4j
 
Rda-What we need to know
Rda-What we need to knowRda-What we need to know
Rda-What we need to know
 
LIS 653, Session 6: FRBR & Relationships
LIS 653, Session 6: FRBR & Relationships LIS 653, Session 6: FRBR & Relationships
LIS 653, Session 6: FRBR & Relationships
 
¿ARCHIVO?
¿ARCHIVO?¿ARCHIVO?
¿ARCHIVO?
 
que hisciste el verano pasado
que hisciste el verano pasadoque hisciste el verano pasado
que hisciste el verano pasado
 

Dernier

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

associations.pptx

  • 4. Ye Olde Internet Server Router Controller View Browser Model DB MVC Model Review What key capabilities do Rails model classes provide?
  • 5. Ye Olde Internet Server Router Controller View Browser Model DB MVC Model Review What key capability do Rails model classes provide? CRUD persistent data
  • 6. Ye Olde Internet Server Router Controller View Browser Model DB MVC Model Review How do you create Rails model classes?
  • 7. Ye Olde Internet Server Router Controller View Browser Model DB MVC Model Review How do you create Rails model classes? Generate DB migrations and model classes with “rails g model …” then customize
  • 8. Ye Olde Internet Server Router Controller View Browser Model DB MVC Model Review What purpose do migrations serve?
  • 9. Ye Olde Internet Server Router Controller View Browser Model DB MVC Model Review What purpose do migrations serve? Setup tables in DB via, e.g., “rails db:migrate” command
  • 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:
  • 18. Rails Model Associations Topics: • OO Class Associations • Rails ORM • How to code in Rails
  • 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:
  • 32. Rails Model Associations Topics: • OO Class Associations • Rails ORM • How to code in Rails
  • 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
  • 42. Rails Relationship Support • has one / belongs to one • has many / belongs to one • join table
  • 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
  • 50. Rails Relationship Support • has one / belongs to one • has many / belongs to one • join table
  • 51. Example Many-to-Many Association name : string Physician name : string Patient * physician * patient appointment_date : datetime Appointment
  • 52. Join Table Example From http://guides.rubyonrails.org/v4.2.0/association_basics.html
  • 53. Join Table Example Join table From http://guides.rubyonrails.org/v4.2.0/association_basics.html
  • 54. Join Table Example References (aka foreign keys) From http://guides.rubyonrails.org/v4.2.0/association_basics.html
  • 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
  • 57. Summary • Model associations • Foreign keys • Rails ORM for assoc. • Has-one/belong-to-one • Has-many/belongs-to-one • Many-to-many join table http://flic.kr/p/aCLor3

Notes de l'éditeur

  1. Painted Shadows - Not Allowed Taken from Alexander Calder's mobile room at the National Gallery of Art, Washington DC.