SlideShare ist ein Scribd-Unternehmen logo
1 von 10
What in the world is
ActiveRecord?
A: (sweet ruby) MAGIC in the form of a Design Pattern!
Let’s start with SQL!
Wait what? That’s not ActiveRecord
• SQL stands for Structured Query Language
• There are a ton of different SQL Databases!!
• SQL lets you manage data in a relational database
• Insert - Create a new row of information in a table
• Update - Change information in an existing row
• Select - Retrieve information in a table
• Delete - Remove a row in a table
• SQL uses data types such as String, Text, Integer,
Date, Time
• An SQL query looks like this:
SELECT * FROM cats WHERE cats.breed = ‘Sphynx’;
How does SQL even…?
Doing it the Ruby Way
Active Record Pattern
Convention over Configuration (CoC)
Don’t Repeat Yourself (DRY)
Model-View-Controller (MVC)
• Some Frameworks require you to write a lot of
configuration code.
• Rails Adopts Conventions so you don’t have to!
1. Naming Conventions
2. Schema Conventions
3. Query Interface
Ruby on Rails Guides: http://guides.rubyonrails.org/active_record_basics.html#convention-over-configuration-in-active-record
What’s an ORM?
• ORM stands for Object Relational Mapper
• Maps database to ruby object
• They get rid of SQL queries in code
SELECT * FROM cats VS Cats.all
• Essentially ActiveRecord takes ruby code and translates
it into SQL code.
Hint: Active Record is Ruby’s way of dealing with ugly SQL!
Active Record is the “M” in MVC
model: DOGGIE
id name color spotted adorable age
1 Chowderhead tan and white FALSE TRUE 1
2 Paco snow white FALSE TRUE 3
3 Tenley brown and
white spots
TRUE TRUE 5
Models are Ruby classes.
• They talk to the database
• Store and validate data
• Performs the logic of knowing
about an instance of itself
So… what is a MODEL anyway??
Active Record Relationships
How Do Models Connect with Each Other?
Lets start easy:
A Supplier has one Account
(This is a One-to-One Relationship)
Alternately,
an Order belongs to a Customer
(Also, a One-to-One Relationship)
More Active Record Relationships
And when a Doctor has many Patients,
but a Patient has many Doctors too!
…and they both have many Appointments!
So Why Associations?
• Make ugly SQL statements Ruby-Friendly:
SELECT surveys FROM surveys JOIN ON users WHERE user.name LIKE “Dicko”
—becomes—
dicko = User.find_by_name(“Dicko”)
dicko.surveys
• We are basically making wonderful Ruby Methods with
each association we declare!
• Remember: IT ONLY LOOKS LIKE MAGIC (it never is)

Weitere ähnliche Inhalte

Andere mochten auch (6)

Los ritmos de aprendizaje
Los ritmos de aprendizajeLos ritmos de aprendizaje
Los ritmos de aprendizaje
 
Staying Secure When Moving to the Cloud - Dave Millier
Staying Secure When Moving to the Cloud - Dave MillierStaying Secure When Moving to the Cloud - Dave Millier
Staying Secure When Moving to the Cloud - Dave Millier
 
Use of evaluation findings; types and influences
Use of evaluation findings; types and influences Use of evaluation findings; types and influences
Use of evaluation findings; types and influences
 
2015 10 21_expo_sem_7-gian franco compagnoni - shotcrete
2015 10 21_expo_sem_7-gian franco compagnoni - shotcrete2015 10 21_expo_sem_7-gian franco compagnoni - shotcrete
2015 10 21_expo_sem_7-gian franco compagnoni - shotcrete
 
Manuel Alegre
Manuel AlegreManuel Alegre
Manuel Alegre
 
CD25
CD25CD25
CD25
 

Ähnlich wie Active Record PowerPoint

Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answers
Kaing Menglieng
 
Databases & Microsoft SQL Server
Databases & Microsoft SQL ServerDatabases & Microsoft SQL Server
Databases & Microsoft SQL Server
Mahmoud Abdallah
 
Sem tech 2010_integrity_constraints
Sem tech 2010_integrity_constraintsSem tech 2010_integrity_constraints
Sem tech 2010_integrity_constraints
Clark & Parsia LLC
 
Validating Linked Data with OWL
Validating Linked Data with OWLValidating Linked Data with OWL
Validating Linked Data with OWL
Clark & Parsia LLC
 
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
The Hive
 

Ähnlich wie Active Record PowerPoint (20)

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
 
SQL Tips + Tricks for Developers
SQL Tips + Tricks for DevelopersSQL Tips + Tricks for Developers
SQL Tips + Tricks for Developers
 
Enterprise NoSQL: Silver Bullet or Poison Pill
Enterprise NoSQL: Silver Bullet or Poison PillEnterprise NoSQL: Silver Bullet or Poison Pill
Enterprise NoSQL: Silver Bullet or Poison Pill
 
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
 
To SQL or NoSQL, that is the question
To SQL or NoSQL, that is the questionTo SQL or NoSQL, that is the question
To SQL or NoSQL, that is the question
 
Sql server common interview questions and answers
Sql server   common interview questions and answersSql server   common interview questions and answers
Sql server common interview questions and answers
 
Rob Harrop- Key Note The God, the Bad and the Ugly - NoSQL matters Paris 2015
Rob Harrop- Key Note The God, the Bad and the Ugly - NoSQL matters Paris 2015Rob Harrop- Key Note The God, the Bad and the Ugly - NoSQL matters Paris 2015
Rob Harrop- Key Note The God, the Bad and the Ugly - NoSQL matters Paris 2015
 
Databases & Microsoft SQL Server
Databases & Microsoft SQL ServerDatabases & Microsoft SQL Server
Databases & Microsoft SQL Server
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...
 
Agile Data: Building Hadoop Analytics Applications
Agile Data: Building Hadoop Analytics ApplicationsAgile Data: Building Hadoop Analytics Applications
Agile Data: Building Hadoop Analytics Applications
 
Domain oriented development
Domain oriented developmentDomain oriented development
Domain oriented development
 
Revision
RevisionRevision
Revision
 
Some NoSQL
Some NoSQLSome NoSQL
Some NoSQL
 
NoSQL
NoSQLNoSQL
NoSQL
 
Allyourbase
AllyourbaseAllyourbase
Allyourbase
 
Introduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregationIntroduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregation
 
Into the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional ProgrammingInto the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional Programming
 
Sem tech 2010_integrity_constraints
Sem tech 2010_integrity_constraintsSem tech 2010_integrity_constraints
Sem tech 2010_integrity_constraints
 
Validating Linked Data with OWL
Validating Linked Data with OWLValidating Linked Data with OWL
Validating Linked Data with OWL
 
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
Agile Data Science by Russell Jurney_ The Hive_Janruary 29 2014
 

Active Record PowerPoint

  • 1. What in the world is ActiveRecord? A: (sweet ruby) MAGIC in the form of a Design Pattern!
  • 2. Let’s start with SQL! Wait what? That’s not ActiveRecord • SQL stands for Structured Query Language • There are a ton of different SQL Databases!!
  • 3. • SQL lets you manage data in a relational database • Insert - Create a new row of information in a table • Update - Change information in an existing row • Select - Retrieve information in a table • Delete - Remove a row in a table • SQL uses data types such as String, Text, Integer, Date, Time • An SQL query looks like this: SELECT * FROM cats WHERE cats.breed = ‘Sphynx’; How does SQL even…?
  • 4. Doing it the Ruby Way Active Record Pattern Convention over Configuration (CoC) Don’t Repeat Yourself (DRY) Model-View-Controller (MVC) • Some Frameworks require you to write a lot of configuration code. • Rails Adopts Conventions so you don’t have to! 1. Naming Conventions 2. Schema Conventions 3. Query Interface Ruby on Rails Guides: http://guides.rubyonrails.org/active_record_basics.html#convention-over-configuration-in-active-record
  • 5. What’s an ORM? • ORM stands for Object Relational Mapper • Maps database to ruby object • They get rid of SQL queries in code SELECT * FROM cats VS Cats.all • Essentially ActiveRecord takes ruby code and translates it into SQL code. Hint: Active Record is Ruby’s way of dealing with ugly SQL!
  • 6. Active Record is the “M” in MVC model: DOGGIE id name color spotted adorable age 1 Chowderhead tan and white FALSE TRUE 1 2 Paco snow white FALSE TRUE 3 3 Tenley brown and white spots TRUE TRUE 5 Models are Ruby classes. • They talk to the database • Store and validate data • Performs the logic of knowing about an instance of itself So… what is a MODEL anyway??
  • 7. Active Record Relationships How Do Models Connect with Each Other? Lets start easy: A Supplier has one Account (This is a One-to-One Relationship)
  • 8. Alternately, an Order belongs to a Customer (Also, a One-to-One Relationship) More Active Record Relationships
  • 9. And when a Doctor has many Patients, but a Patient has many Doctors too! …and they both have many Appointments!
  • 10. So Why Associations? • Make ugly SQL statements Ruby-Friendly: SELECT surveys FROM surveys JOIN ON users WHERE user.name LIKE “Dicko” —becomes— dicko = User.find_by_name(“Dicko”) dicko.surveys • We are basically making wonderful Ruby Methods with each association we declare! • Remember: IT ONLY LOOKS LIKE MAGIC (it never is)