SlideShare una empresa de Scribd logo
1 de 17
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

Más contenido relacionado

Similar a Active Record Data Modeling

Databases & Microsoft SQL Server
Databases & Microsoft SQL ServerDatabases & Microsoft SQL Server
Databases & Microsoft SQL Server
Mahmoud Abdallah
 
Active Record PowerPoint
Active Record PowerPointActive Record PowerPoint
Active Record PowerPoint
Elizabeth Cruz
 

Similar a 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
 
Neo4j Presentation
Neo4j PresentationNeo4j Presentation
Neo4j Presentation
 
Thinking about graphs
Thinking about graphsThinking about graphs
Thinking about graphs
 
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
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
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...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

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