0
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 migr...
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_masterWHER...
RAILS CONSOLE>> ActiveRecord::Base.logger = Logger.new(STDOUT)>> Person.first SQL (1.0ms) SELECT nameFROM sqlite_masterWHER...
RAILS CONSOLE>> ActiveRecord::Base.logger = Logger.new(STDOUT)>> Person.first SQL (1.0ms) SELECT nameFROM sqlite_masterWHER...
DYNAMIC GETTERS AND            SETTERS>> bret = Person.new>> bret.first_name = "Bret">> bret.last_name = "Smith">> bret.firs...
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, :...
QUESTIONS?
Upcoming SlideShare
Loading in...5
×

ActiveRecord

764

Published on

An overview of ActiveRecord (Rails 3)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
764
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "ActiveRecord"

    1. 1. ACTIVERECORD Rails 3
    2. 2. MVC
    3. 3. MODEL CLASSin app/models/person.rbclass Message < ActiveRecord::Baseend
    4. 4. EVERY MODEL HASA CORRESPONDING DATABASE TABLE
    5. 5. MVC
    6. 6. HOW DID WE GETA DATABASE TABLE?
    7. 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...
    8. 8. DEMO
    9. 9. RAILS CONSOLE>> ActiveRecord::Base.logger = Logger.new(STDOUT)
    10. 10. 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">
    11. 11. 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
    12. 12. 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"
    13. 13. DYNAMIC GETTERS AND SETTERS>> bret = Person.new>> bret.first_name = "Bret">> bret.last_name = "Smith">> bret.first_name"Bret">> bret.xxxNoMethodError
    14. 14. MORE ACTIVE RECORD• save to database as needed• automatically handle update/insert• dynamic queries
    15. 15. SQL INJECTION
    16. 16. 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
    17. 17. QUESTIONS?
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×