SlideShare a Scribd company logo
1 of 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

More Related Content

Similar to 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
 

Similar to 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
 

Recently uploaded

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

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

Editor's Notes

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