• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
ActiveRecord
 

ActiveRecord

on

  • 936 views

An overview of ActiveRecord (Rails 3)

An overview of ActiveRecord (Rails 3)

Statistics

Views

Total Views
936
Views on SlideShare
936
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

ActiveRecord ActiveRecord Presentation Transcript

  • ACTIVERECORD Rails 3
  • MVC
  • MODEL CLASSin app/models/person.rbclass Message < ActiveRecord::Baseend
  • EVERY MODEL HASA CORRESPONDING DATABASE TABLE
  • MVC
  • HOW DID WE GETA 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 nameFROM sqlite_masterWHERE 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 nameFROM sqlite_masterWHERE 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 nameFROM sqlite_masterWHERE 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.xxxNoMethodError
  • MORE ACTIVE RECORD• save to database as needed• automatically handle update/insert• dynamic queries
  • SQL INJECTION
  • SAFE FROM SQL INJECTIONclass 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?