SlideShare a Scribd company logo
Basic Active Record
Relationships
One-to-one, one-to-many, many-to-many
One to One
▪ The table of the belongs_to
model contains a column with
the foreign key (often id) of the
has_one model
▪ Not always clear when to use
one
▪ Users and social media profiles
– A user only has_one Facebook
profile, and that profile only
belongs_to that user
So when do I use it?
One indicator: the potential for
a lot of null (empty) fields
One to Many
▪ Very common relationship
▪ Like one to one, the table of the belongs_to model contains a column
with the foreign key (often id) of the has_many model
▪ Users and orders
– A user has_many orders, and each order only belongs_to that user
Many to Many
▪ Also very common
▪ Writing prompts and categories
– Categories can contain many prompts, and those prompts can belong to many
categories
▪ Two ways of describing the relationship
– has_many JOINTABLE & has_manyOTHERTABLE through JOINTABLE
– has_and_belongs_to_many (Rails/ActiveRecord magic creates the join table)
▪ Join table contains the foreign keys (ids) of what's connected to it
When to use has_and_belongs_to_many
When your join table only contains the foreign keys(ids)
and no other useful information
When to use has_and_belongs_to_many
When your join table only contains the foreign keys(ids)
and no other useful information
not conventional
When to use has_and_belongs_to_many
When your join table only contains the foreign keys(ids)
and no other useful information
t.what_now?
▪ t.belongs_to, t.references, and t.integer
– Everyone has their own preference, but any of these will work
– t.belongs_to :user
– t.references :user
– t.integer :user_id
▪ Note: when you use t.integer you need to specify the id because integer just means a
number
▪ With t.belongs_to or t.references the Rails/ActiveRecord magic can safely assume
what you mean
More resources
▪ http://www.mutuallyhuman.com/blog/2014/06/12/activerecord-
associations-and-you/
▪ http://guides.rubyonrails.org/association_basics.html
▪ https://github.com/kdmcclin/schema-practice
▪ https://github.com/kdmcclin/chained-relationships
@scarletalphabet

More Related Content

Similar to Basic Active Record

ORACLE: Database management system student
ORACLE: Database management system studentORACLE: Database management system student
ORACLE: Database management system studentjomerson remorosa
 
Related Worksheets
Related WorksheetsRelated Worksheets
Related WorksheetsEirik Bakke
 
ER-MODEL WITH DBMS.pptx
ER-MODEL WITH DBMS.pptxER-MODEL WITH DBMS.pptx
ER-MODEL WITH DBMS.pptxKomal Komal
 

Similar to Basic Active Record (6)

ORACLE: Database management system student
ORACLE: Database management system studentORACLE: Database management system student
ORACLE: Database management system student
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEM
 
Related Worksheets
Related WorksheetsRelated Worksheets
Related Worksheets
 
er-models.pptx
er-models.pptxer-models.pptx
er-models.pptx
 
Data Models
Data ModelsData Models
Data Models
 
ER-MODEL WITH DBMS.pptx
ER-MODEL WITH DBMS.pptxER-MODEL WITH DBMS.pptx
ER-MODEL WITH DBMS.pptx
 

Recently uploaded

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfOrtus Solutions, Corp
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesNeo4j
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfMeon Technology
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowPeter Caitens
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Gáspár Nagy
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEJelle | Nordend
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationHelp Desk Migration
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockSkilrock Technologies
 
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems ApproachNeo4j
 
How To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdfHow To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdfayushiqss
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisNeo4j
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownloadvrstrong314
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Krakówbim.edu.pl
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessWSO2
 

Recently uploaded (20)

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
How To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdfHow To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdf
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 

Basic Active Record

  • 1.
  • 3. One to One ▪ The table of the belongs_to model contains a column with the foreign key (often id) of the has_one model ▪ Not always clear when to use one ▪ Users and social media profiles – A user only has_one Facebook profile, and that profile only belongs_to that user
  • 4.
  • 5. So when do I use it? One indicator: the potential for a lot of null (empty) fields
  • 6. One to Many ▪ Very common relationship ▪ Like one to one, the table of the belongs_to model contains a column with the foreign key (often id) of the has_many model ▪ Users and orders – A user has_many orders, and each order only belongs_to that user
  • 7.
  • 8.
  • 9. Many to Many ▪ Also very common ▪ Writing prompts and categories – Categories can contain many prompts, and those prompts can belong to many categories ▪ Two ways of describing the relationship – has_many JOINTABLE & has_manyOTHERTABLE through JOINTABLE – has_and_belongs_to_many (Rails/ActiveRecord magic creates the join table) ▪ Join table contains the foreign keys (ids) of what's connected to it
  • 10.
  • 11. When to use has_and_belongs_to_many When your join table only contains the foreign keys(ids) and no other useful information
  • 12. When to use has_and_belongs_to_many When your join table only contains the foreign keys(ids) and no other useful information not conventional
  • 13. When to use has_and_belongs_to_many When your join table only contains the foreign keys(ids) and no other useful information
  • 14. t.what_now? ▪ t.belongs_to, t.references, and t.integer – Everyone has their own preference, but any of these will work – t.belongs_to :user – t.references :user – t.integer :user_id ▪ Note: when you use t.integer you need to specify the id because integer just means a number ▪ With t.belongs_to or t.references the Rails/ActiveRecord magic can safely assume what you mean
  • 15. More resources ▪ http://www.mutuallyhuman.com/blog/2014/06/12/activerecord- associations-and-you/ ▪ http://guides.rubyonrails.org/association_basics.html ▪ https://github.com/kdmcclin/schema-practice ▪ https://github.com/kdmcclin/chained-relationships @scarletalphabet