SlideShare a Scribd company logo
1 of 17
ACTIVERECORD
     Rails 3
MVC
MODEL CLASS


in app/models/person.rb



class Message < ActiveRecord::Base

end
EVERY MODEL
       HAS
A CORRESPONDING
  DATABASE TABLE
MVC
HOW DID WE
      GET
A DATABASE TABLE?
RUBY-DRIVEN SCHEMA

•   scaffold generates
    db/migrate/20110131021702_create_people.rb

•   rake db:migrate
    runs the migration, creates the table



•   Watch what we did before in slow motion...
DEMO
RAILS CONSOLE


>> ActiveRecord::Base.logger = Logger.new(STDOUT)
RAILS CONSOLE
>> ActiveRecord::Base.logger = Logger.new(STDOUT)
>> Person.first
 SQL (1.0ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
 Person Load (1.0ms) SELECT "people".* FROM "people" LIMIT 1
=> #<Person id: 2, first_name: "Wilma", last_name: "Flinstone",
  created_at: "2010-10-19 19:59:36", updated_at: "2010-10-19
  19:59:36">
RAILS CONSOLE
>> ActiveRecord::Base.logger = Logger.new(STDOUT)
>> Person.first
 SQL (1.0ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
 Person Load (1.0ms) SELECT "people".* FROM "people" LIMIT 1
=> #<Person id: 2, first_name: "Wilma", last_name: "Flinstone",
  created_at: "2010-10-19 19:59:36", updated_at: "2010-10-19
  19:59:36">




                 find "people" table name
RAILS CONSOLE
>> ActiveRecord::Base.logger = Logger.new(STDOUT)
>> Person.first
 SQL (1.0ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
 Person Load (1.0ms) SELECT "people".* FROM "people" LIMIT 1
=> #<Person id: 2, first_name: "Wilma", last_name: "Flinstone",
  created_at: "2010-10-19 19:59:36", updated_at: "2010-10-19
  19:59:36">




               find first row in "people table"
DYNAMIC GETTERS AND
            SETTERS
>> bret = Person.new
>> bret.first_name = "Bret"
>> bret.last_name = "Smith"
>> bret.first_name
"Bret"
>> bret.xxx
NoMethodError
MORE ACTIVE RECORD
• save to database as needed
• automatically handle update/insert
• dynamic queries
SQL INJECTION
SAFE FROM SQL INJECTION
class User < ActiveRecord::Base
  def self.authenticate_unsafely(user_name, password)
   find(:first, :conditions =>
         "user_name = '#{user_name}' AND password = '#{password}'")
  end

  def self.authenticate_safely(user_name, password)
   find(:first, :conditions =>
      [ "user_name = ? AND password = ?", user_name, password ])
  end

  def self.authenticate_safely_simply(user_name, password)
   find(:first, :conditions =>
        { :user_name => user_name, :password => password })
  end
QUESTIONS?

More Related Content

What's hot

Rapid postgresql learning, part 1
Rapid postgresql learning, part 1Rapid postgresql learning, part 1
Rapid postgresql learning, part 1
Ali MasudianPour
 
Rapid postgresql learning, part 2
Rapid postgresql learning, part 2Rapid postgresql learning, part 2
Rapid postgresql learning, part 2
Ali MasudianPour
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and Cassandra
Deependra Ariyadewa
 

What's hot (20)

Single Sign-On with Waffle
Single Sign-On with WaffleSingle Sign-On with Waffle
Single Sign-On with Waffle
 
TDD in the wild
TDD in the wildTDD in the wild
TDD in the wild
 
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
MooseX::Datamodel - Barcelona Perl Workshop Lightning talkMooseX::Datamodel - Barcelona Perl Workshop Lightning talk
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
 
WAFFLE: Windows Authentication in Java
WAFFLE: Windows Authentication in JavaWAFFLE: Windows Authentication in Java
WAFFLE: Windows Authentication in Java
 
Database connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwariDatabase connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwari
 
RegistryModClass
RegistryModClassRegistryModClass
RegistryModClass
 
React Spotlight 2018
React Spotlight 2018React Spotlight 2018
React Spotlight 2018
 
Rapid postgresql learning, part 1
Rapid postgresql learning, part 1Rapid postgresql learning, part 1
Rapid postgresql learning, part 1
 
Couchdb
CouchdbCouchdb
Couchdb
 
Excel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a serverExcel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a server
 
Rapid postgresql learning, part 2
Rapid postgresql learning, part 2Rapid postgresql learning, part 2
Rapid postgresql learning, part 2
 
Mule esb object_to_json
Mule esb object_to_jsonMule esb object_to_json
Mule esb object_to_json
 
Muleesbobjecttojson
Muleesbobjecttojson Muleesbobjecttojson
Muleesbobjecttojson
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and Cassandra
 
CRUD VB2010
CRUD VB2010CRUD VB2010
CRUD VB2010
 
PHP Database Programming Basics -- Northeast PHP
PHP Database Programming Basics -- Northeast PHPPHP Database Programming Basics -- Northeast PHP
PHP Database Programming Basics -- Northeast PHP
 
Lab2-DB-Mongodb
Lab2-DB-MongodbLab2-DB-Mongodb
Lab2-DB-Mongodb
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
 
Mongo db
Mongo dbMongo db
Mongo db
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 

Similar to ActiveRecord

Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Alex Sharp
 

Similar to ActiveRecord (20)

Databases with SQLite3.pdf
Databases with SQLite3.pdfDatabases with SQLite3.pdf
Databases with SQLite3.pdf
 
The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202
 
The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.10 book - Part 37 of 212The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.10 book - Part 37 of 212
 
Using web2py's DAL in other projects or frameworks
Using web2py's DAL in other projects or frameworksUsing web2py's DAL in other projects or frameworks
Using web2py's DAL in other projects or frameworks
 
The Ring programming language version 1.3 book - Part 20 of 88
The Ring programming language version 1.3 book - Part 20 of 88The Ring programming language version 1.3 book - Part 20 of 88
The Ring programming language version 1.3 book - Part 20 of 88
 
The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189
 
The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196
 
The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180
 
DEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq liteDEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq lite
 
The Ring programming language version 1.5.4 book - Part 29 of 185
The Ring programming language version 1.5.4 book - Part 29 of 185The Ring programming language version 1.5.4 book - Part 29 of 185
The Ring programming language version 1.5.4 book - Part 29 of 185
 
The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31
 
The Ring programming language version 1.5.2 book - Part 28 of 181
The Ring programming language version 1.5.2 book - Part 28 of 181The Ring programming language version 1.5.2 book - Part 28 of 181
The Ring programming language version 1.5.2 book - Part 28 of 181
 
The Ring programming language version 1.9 book - Part 36 of 210
The Ring programming language version 1.9 book - Part 36 of 210The Ring programming language version 1.9 book - Part 36 of 210
The Ring programming language version 1.9 book - Part 36 of 210
 
The Ring programming language version 1.2 book - Part 18 of 84
The Ring programming language version 1.2 book - Part 18 of 84The Ring programming language version 1.2 book - Part 18 of 84
The Ring programming language version 1.2 book - Part 18 of 84
 
The Ring programming language version 1.5.3 book - Part 29 of 184
The Ring programming language version 1.5.3 book - Part 29 of 184The Ring programming language version 1.5.3 book - Part 29 of 184
The Ring programming language version 1.5.3 book - Part 29 of 184
 
&lt;x> Rails Web App Security Title
&lt;x> Rails Web App Security Title&lt;x> Rails Web App Security Title
&lt;x> Rails Web App Security Title
 
Session #5 content providers
Session #5  content providersSession #5  content providers
Session #5 content providers
 
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 

More from Blazing Cloud

RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
Blazing Cloud
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
Blazing Cloud
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Blazing Cloud
 

More from Blazing Cloud (20)

Rails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyRails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_many
 
Active Record Introduction - 3
Active Record Introduction - 3Active Record Introduction - 3
Active Record Introduction - 3
 
Rails Class Intro - 1
Rails Class Intro - 1 Rails Class Intro - 1
Rails Class Intro - 1
 
Your first rails app - 2
 Your first rails app - 2 Your first rails app - 2
Your first rails app - 2
 
RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
 
Extending rails
Extending railsExtending rails
Extending rails
 
2day Ruby Class Intro
2day Ruby Class Intro2day Ruby Class Intro
2day Ruby Class Intro
 
Mobile Lean UX
Mobile Lean UXMobile Lean UX
Mobile Lean UX
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
 
Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3
 
Form helpers
Form helpersForm helpers
Form helpers
 
Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)
 
What you don't know (yet)
What you don't know (yet)What you don't know (yet)
What you don't know (yet)
 
Introduction to Rails
Introduction to RailsIntroduction to Rails
Introduction to Rails
 
Ruby on Rails Class intro
Ruby on Rails Class introRuby on Rails Class intro
Ruby on Rails Class intro
 
Ruby on rails toolbox
Ruby on rails toolboxRuby on rails toolbox
Ruby on rails toolbox
 
Routes Controllers
Routes ControllersRoutes Controllers
Routes Controllers
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Enumerables
EnumerablesEnumerables
Enumerables
 
Reg EX
Reg EXReg EX
Reg EX
 

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 

ActiveRecord

  • 1. ACTIVERECORD Rails 3
  • 2. MVC
  • 3. MODEL CLASS in app/models/person.rb class Message < ActiveRecord::Base end
  • 4. EVERY MODEL HAS A CORRESPONDING DATABASE TABLE
  • 5. MVC
  • 6. HOW DID WE GET A DATABASE TABLE?
  • 7. RUBY-DRIVEN SCHEMA • scaffold generates db/migrate/20110131021702_create_people.rb • rake db:migrate runs the migration, creates the table • Watch what we did before in slow motion...
  • 10. RAILS CONSOLE >> ActiveRecord::Base.logger = Logger.new(STDOUT) >> Person.first SQL (1.0ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' Person Load (1.0ms) SELECT "people".* FROM "people" LIMIT 1 => #<Person id: 2, first_name: "Wilma", last_name: "Flinstone", created_at: "2010-10-19 19:59:36", updated_at: "2010-10-19 19:59:36">
  • 11. RAILS CONSOLE >> ActiveRecord::Base.logger = Logger.new(STDOUT) >> Person.first SQL (1.0ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' Person Load (1.0ms) SELECT "people".* FROM "people" LIMIT 1 => #<Person id: 2, first_name: "Wilma", last_name: "Flinstone", created_at: "2010-10-19 19:59:36", updated_at: "2010-10-19 19:59:36"> find "people" table name
  • 12. RAILS CONSOLE >> ActiveRecord::Base.logger = Logger.new(STDOUT) >> Person.first SQL (1.0ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' Person Load (1.0ms) SELECT "people".* FROM "people" LIMIT 1 => #<Person id: 2, first_name: "Wilma", last_name: "Flinstone", created_at: "2010-10-19 19:59:36", updated_at: "2010-10-19 19:59:36"> find first row in "people table"
  • 13. DYNAMIC GETTERS AND SETTERS >> bret = Person.new >> bret.first_name = "Bret" >> bret.last_name = "Smith" >> bret.first_name "Bret" >> bret.xxx NoMethodError
  • 14. MORE ACTIVE RECORD • save to database as needed • automatically handle update/insert • dynamic queries
  • 16. SAFE FROM SQL INJECTION class User < ActiveRecord::Base def self.authenticate_unsafely(user_name, password) find(:first, :conditions => "user_name = '#{user_name}' AND password = '#{password}'") end def self.authenticate_safely(user_name, password) find(:first, :conditions => [ "user_name = ? AND password = ?", user_name, password ]) end def self.authenticate_safely_simply(user_name, password) find(:first, :conditions => { :user_name => user_name, :password => password }) end

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n