SlideShare a Scribd company logo
Data Modeling WITH ACTIVE
RECORD IN RAILS
About Me:
Hannah Howard
Twitter: @techgirlwonder
Email: hannah@techgirlwonder.com
I run my own computer services business called
Tech Girl Wonder.And I’m hiring.
I do ruby programming with Logical Reality Design,
a Rails development shop. Hire us!
• This is my take on the subject, not the one true way.
• For the smarty pants in the room, I’m skipping over a lot.
Possibly even saying things that aren’t always true.
• There will be time for questions.
• This is not a lecture, only the first 15-30 minutes are.
A Few CAVEATS
Why This Talk?
• I think I’ve noticed some patterns in where new people get
stuck learning Rails
• I think it has to do with the parts of Rails that are closer to
traditional computer programming and very different from
HTML/CSS
• Specifically, I think people get stuck on the ‘M’ in the MVC,
i.e ActiveRecord and its connection to an SQL database.
STATIC WebSites
VS
Web Applications
Web Applications
Web Applications
Static Web Sites
Server
Give me a
page!
Here’s a
page!
Client
Static web page: is delivered
to the user exactly as stored.
Web APPLICATION
Informatio
n
Presented
To The
User
Data
Retrieved
From The
Model
Model Of
Data
Represent
ed by App
List of
changes
sent to
the model
Interface
For
Receiving
Input
ServerClient View Controller
Gives Input
Sees Results
WHAT IS THE MODEL?
• A model of data represented by the application
• Rails stores its data in a relational database. (usually)
• You access data in relational databases with SQL queries
• SQL queries are complex and hard to write
• ActiveRecord is there to make that easier and “better”
What is A RELATIONAL
DATABASE?
• A series of data tables (like Excel spreadsheets) that are
connected to each other through relationships
• Hint: Spend some time with Filemaker or Access
RelationAL Database Example
Id Weight Stale
1 6oz TRUE
2 8oz FALSE
3 5oz FALSE
Id Name Country
1 Swiss Switzerland
2 American U.S.
3 Harvarty Denmark
Id Name Address
1 Cheesetopia 123 Fake St.
2 I Love Cheese 8675 Threeonine Ave.
3 Bob’s Cheeses 1 Infinite Loop
Cheese Types
Cheeses
Cheese Shops
Id Weight Stale TypeID ShopID
1 6oz TRUE 1 2
2 8oz FALSE 3 3
3 5oz FALSE 2 1
Have
Many Have
Many
Have
Many
Have
Many
Belongs
To
Belongs
To
TypeID ShopID
3 2
1 2
2 1
1 1
3 3
2 3
SQL: Standard Query Lanaguage
SELECT * FROM CHEESES
INNER JOIN
CHEESE_TYPES ON
cheeses.cheese_type_id =
cheese_types.id WHERE
cheese_types.name IN
[“SWISS”,“HARVARTI”]
WHERE cheeses.stale =
FALSE
AcTIVERECORD
Cheese Types
class CheeseType < ActiveRecord::Base
has_many :cheeses
has_and_belongs_to_many :cheese_stores
attr_accessible :name, :country
end
Cheeses
class Cheese < ActiveRecord::Base
belongs_to :cheese_type
belongs_to :cheese_store
attr_accessible :weight, :stale
end
Cheese Stores
class CheeseStore < ActiveRecord::Base
has_many :cheeses
has_and_belongs_to_many :cheese_types
attr_accessible :name, :address
WoRKING WITH ACTIVE
RECORD
cheese_type.country = “France”
cheese.cheese_store
cheese_store.cheese_types
Cheese.where(:weight => 4.0..10.0)
Cheese.joins(:cheese_types).where(cheese_types: {name:
[“SWISS”,“HAVARTI”]}).where(:stale => false)
The KEY PIECE OF GLUE:
Migrationsrails generate model cheeses weight:decimal stale:boolean cheese_type_id:integer
cheese_store_id:integer
class CreateCheeses < ActiveRecord::Migration
def change
create_table :cheeses do |t|
t.decimal :weight
t.boolean :stale
t.integer :cheese_type_id
t.integer :cheese_store_id
t.timestamps
end
end
end
Oh wait, so that’s what rake db:migrate does!
Are You READY TO START
PROGRAMMING?
rb-tunes
Our Project:
iTunes
Lets Start Making It!
Tracks
Albums
Artists
Playlists

More Related Content

Similar to Active Record Data Modeling

Semantic Graph Databases: The Evolution of Relational Databases
Semantic Graph Databases: The Evolution of Relational DatabasesSemantic Graph Databases: The Evolution of Relational Databases
Semantic Graph Databases: The Evolution of Relational Databases
Cambridge Semantics
 
SMU No SQL Talk
SMU No SQL TalkSMU No SQL Talk
SMU No SQL Talk
Justin Weinberg
 
Lessons learnt coverting from SQL to NoSQL
Lessons learnt coverting from SQL to NoSQLLessons learnt coverting from SQL to NoSQL
Lessons learnt coverting from SQL to NoSQL
Enda Farrell
 
Neo4j Data Science Presentation
Neo4j Data Science PresentationNeo4j Data Science Presentation
Neo4j Data Science Presentation
Max De Marzi
 
Databases & Microsoft SQL Server
Databases & Microsoft SQL ServerDatabases & Microsoft SQL Server
Databases & Microsoft SQL ServerMahmoud Abdallah
 
Agile velocity - Requirements Discovery Presentation
Agile velocity  - Requirements Discovery Presentation Agile velocity  - Requirements Discovery Presentation
Agile velocity - Requirements Discovery Presentation
David Hawks
 
Blazing Data With Redis (and LEGOS!)
Blazing Data With Redis (and LEGOS!)Blazing Data With Redis (and LEGOS!)
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
Neo4j Training Modeling
Neo4j Training ModelingNeo4j Training Modeling
Neo4j Training Modeling
Max De Marzi
 
Thinking about graphs
Thinking about graphsThinking about graphs
Thinking about graphs
Neo4j
 
Neo4j Presentation
Neo4j PresentationNeo4j Presentation
Neo4j Presentation
Max De Marzi
 
Lightning Talk: What You Need to Know Before You Shard in 20 Minutes
Lightning Talk: What You Need to Know Before You Shard in 20 MinutesLightning Talk: What You Need to Know Before You Shard in 20 Minutes
Lightning Talk: What You Need to Know Before You Shard in 20 Minutes
MongoDB
 
Sharding why,what,when, how
Sharding   why,what,when, howSharding   why,what,when, how
Sharding why,what,when, how
David Murphy
 
Active Record PowerPoint
Active Record PowerPointActive Record PowerPoint
Active Record PowerPointElizabeth Cruz
 
NoSQL: An Analysis
NoSQL: An AnalysisNoSQL: An Analysis
NoSQL: An Analysis
Andrew Brust
 
Real World Performance - OLTP
Real World Performance - OLTPReal World Performance - OLTP
Real World Performance - OLTP
Connor McDonald
 
Cassandra Summit 2014: Fuzzy Entity Matching at Scale
Cassandra Summit 2014: Fuzzy Entity Matching at ScaleCassandra Summit 2014: Fuzzy Entity Matching at Scale
Cassandra Summit 2014: Fuzzy Entity Matching at Scale
DataStax Academy
 
Developing WordPress Plugins Using the MVC Methodology
Developing WordPress Plugins Using the MVC MethodologyDeveloping WordPress Plugins Using the MVC Methodology
Developing WordPress Plugins Using the MVC Methodology
Nate Allen
 
Unlock the value in your big data reservoir using oracle big data discovery a...
Unlock the value in your big data reservoir using oracle big data discovery a...Unlock the value in your big data reservoir using oracle big data discovery a...
Unlock the value in your big data reservoir using oracle big data discovery a...
Mark Rittman
 
Build a modern data platform.pptx
Build a modern data platform.pptxBuild a modern data platform.pptx
Build a modern data platform.pptx
Ike Ellis
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
Davide Mauri
 

Similar to Active Record Data Modeling (20)

Semantic Graph Databases: The Evolution of Relational Databases
Semantic Graph Databases: The Evolution of Relational DatabasesSemantic Graph Databases: The Evolution of Relational Databases
Semantic Graph Databases: The Evolution of Relational Databases
 
SMU No SQL Talk
SMU No SQL TalkSMU No SQL Talk
SMU No SQL Talk
 
Lessons learnt coverting from SQL to NoSQL
Lessons learnt coverting from SQL to NoSQLLessons learnt coverting from SQL to NoSQL
Lessons learnt coverting from SQL to NoSQL
 
Neo4j Data Science Presentation
Neo4j Data Science PresentationNeo4j Data Science Presentation
Neo4j Data Science Presentation
 
Databases & Microsoft SQL Server
Databases & Microsoft SQL ServerDatabases & Microsoft SQL Server
Databases & Microsoft SQL Server
 
Agile velocity - Requirements Discovery Presentation
Agile velocity  - Requirements Discovery Presentation Agile velocity  - Requirements Discovery Presentation
Agile velocity - Requirements Discovery Presentation
 
Blazing Data With Redis (and LEGOS!)
Blazing Data With Redis (and LEGOS!)Blazing Data With Redis (and LEGOS!)
Blazing Data With Redis (and LEGOS!)
 
Neo4j Training Modeling
Neo4j Training ModelingNeo4j Training Modeling
Neo4j Training Modeling
 
Thinking about graphs
Thinking about graphsThinking about graphs
Thinking about graphs
 
Neo4j Presentation
Neo4j PresentationNeo4j Presentation
Neo4j Presentation
 
Lightning Talk: What You Need to Know Before You Shard in 20 Minutes
Lightning Talk: What You Need to Know Before You Shard in 20 MinutesLightning Talk: What You Need to Know Before You Shard in 20 Minutes
Lightning Talk: What You Need to Know Before You Shard in 20 Minutes
 
Sharding why,what,when, how
Sharding   why,what,when, howSharding   why,what,when, how
Sharding why,what,when, how
 
Active Record PowerPoint
Active Record PowerPointActive Record PowerPoint
Active Record PowerPoint
 
NoSQL: An Analysis
NoSQL: An AnalysisNoSQL: An Analysis
NoSQL: An Analysis
 
Real World Performance - OLTP
Real World Performance - OLTPReal World Performance - OLTP
Real World Performance - OLTP
 
Cassandra Summit 2014: Fuzzy Entity Matching at Scale
Cassandra Summit 2014: Fuzzy Entity Matching at ScaleCassandra Summit 2014: Fuzzy Entity Matching at Scale
Cassandra Summit 2014: Fuzzy Entity Matching at Scale
 
Developing WordPress Plugins Using the MVC Methodology
Developing WordPress Plugins Using the MVC MethodologyDeveloping WordPress Plugins Using the MVC Methodology
Developing WordPress Plugins Using the MVC Methodology
 
Unlock the value in your big data reservoir using oracle big data discovery a...
Unlock the value in your big data reservoir using oracle big data discovery a...Unlock the value in your big data reservoir using oracle big data discovery a...
Unlock the value in your big data reservoir using oracle big data discovery a...
 
Build a modern data platform.pptx
Build a modern data platform.pptxBuild a modern data platform.pptx
Build a modern data platform.pptx
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 

Recently uploaded

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 

Recently uploaded (20)

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 

Active Record Data Modeling

  • 1. Data Modeling WITH ACTIVE RECORD IN RAILS
  • 2. About Me: Hannah Howard Twitter: @techgirlwonder Email: hannah@techgirlwonder.com I run my own computer services business called Tech Girl Wonder.And I’m hiring. I do ruby programming with Logical Reality Design, a Rails development shop. Hire us!
  • 3. • This is my take on the subject, not the one true way. • For the smarty pants in the room, I’m skipping over a lot. Possibly even saying things that aren’t always true. • There will be time for questions. • This is not a lecture, only the first 15-30 minutes are. A Few CAVEATS
  • 4. Why This Talk? • I think I’ve noticed some patterns in where new people get stuck learning Rails • I think it has to do with the parts of Rails that are closer to traditional computer programming and very different from HTML/CSS • Specifically, I think people get stuck on the ‘M’ in the MVC, i.e ActiveRecord and its connection to an SQL database.
  • 5. STATIC WebSites VS Web Applications Web Applications Web Applications
  • 6. Static Web Sites Server Give me a page! Here’s a page! Client Static web page: is delivered to the user exactly as stored.
  • 7. Web APPLICATION Informatio n Presented To The User Data Retrieved From The Model Model Of Data Represent ed by App List of changes sent to the model Interface For Receiving Input ServerClient View Controller Gives Input Sees Results
  • 8. WHAT IS THE MODEL? • A model of data represented by the application • Rails stores its data in a relational database. (usually) • You access data in relational databases with SQL queries • SQL queries are complex and hard to write • ActiveRecord is there to make that easier and “better”
  • 9. What is A RELATIONAL DATABASE? • A series of data tables (like Excel spreadsheets) that are connected to each other through relationships • Hint: Spend some time with Filemaker or Access
  • 10. RelationAL Database Example Id Weight Stale 1 6oz TRUE 2 8oz FALSE 3 5oz FALSE Id Name Country 1 Swiss Switzerland 2 American U.S. 3 Harvarty Denmark Id Name Address 1 Cheesetopia 123 Fake St. 2 I Love Cheese 8675 Threeonine Ave. 3 Bob’s Cheeses 1 Infinite Loop Cheese Types Cheeses Cheese Shops Id Weight Stale TypeID ShopID 1 6oz TRUE 1 2 2 8oz FALSE 3 3 3 5oz FALSE 2 1 Have Many Have Many Have Many Have Many Belongs To Belongs To TypeID ShopID 3 2 1 2 2 1 1 1 3 3 2 3
  • 11. SQL: Standard Query Lanaguage SELECT * FROM CHEESES INNER JOIN CHEESE_TYPES ON cheeses.cheese_type_id = cheese_types.id WHERE cheese_types.name IN [“SWISS”,“HARVARTI”] WHERE cheeses.stale = FALSE
  • 12. AcTIVERECORD Cheese Types class CheeseType < ActiveRecord::Base has_many :cheeses has_and_belongs_to_many :cheese_stores attr_accessible :name, :country end Cheeses class Cheese < ActiveRecord::Base belongs_to :cheese_type belongs_to :cheese_store attr_accessible :weight, :stale end Cheese Stores class CheeseStore < ActiveRecord::Base has_many :cheeses has_and_belongs_to_many :cheese_types attr_accessible :name, :address
  • 13. WoRKING WITH ACTIVE RECORD cheese_type.country = “France” cheese.cheese_store cheese_store.cheese_types Cheese.where(:weight => 4.0..10.0) Cheese.joins(:cheese_types).where(cheese_types: {name: [“SWISS”,“HAVARTI”]}).where(:stale => false)
  • 14. The KEY PIECE OF GLUE: Migrationsrails generate model cheeses weight:decimal stale:boolean cheese_type_id:integer cheese_store_id:integer class CreateCheeses < ActiveRecord::Migration def change create_table :cheeses do |t| t.decimal :weight t.boolean :stale t.integer :cheese_type_id t.integer :cheese_store_id t.timestamps end end end Oh wait, so that’s what rake db:migrate does!
  • 15. Are You READY TO START PROGRAMMING?
  • 17. Lets Start Making It! Tracks Albums Artists Playlists