0
ACTIVERECORD                               Rails 3Sunday, November 13, 11
MVCSunday, November 13, 11
MODEL CLASS     in app/models/person.rb     class Message < ActiveRecord::Base     endSunday, November 13, 11
EVERY MODEL                                 HAS                          A CORRESPONDING                            DATABA...
MVCSunday, November 13, 11
HOW DID WE                            GET                      A DATABASE TABLE?Sunday, November 13, 11
RUBY-DRIVEN SCHEMA  •   scaffold generates      db/migrate/20110131021702_create_people.rb  •   rake db:migrate      runs ...
DEMOSunday, November 13, 11
$ rails new ar_exSunday, November 13, 11
$ rails -vSunday, November 13, 11
Edit GemfileSunday, November 13, 11
source http://rubygems.org   gem ‘rails’   gem ‘rspec-rails’   gem ‘sqlite3’Sunday, November 13, 11
$ bundleSunday, November 13, 11
$ rails g rspec:installSunday, November 13, 11
$ rails g model Message               body:text public_key:textSunday, November 13, 11
invoke   rspecSunday, November 13, 11
create   spec/models/                      message_spec.rbSunday, November 13, 11
require spec_helper  describe Message do   pending "a class in itself"  endSunday, November 13, 11
invoke active_recordSunday, November 13, 11
create db/migrate/            20110208201036_create_messages.rbSunday, November 13, 11
class CreateMessages < ActiveRecord::Migration    def self.change    # rake db:migrate    end    def self.up      # rake d...
create_table :messages do |t|                             t.text :body                             t.text :public_key     ...
drop_table :messagesSunday, November 13, 11
http://apidock.com/rails/ActiveRecord/             ConnectionAdapters/SchemaStatementsSunday, November 13, 11
create               app/models/message.rbSunday, November 13, 11
class Message < ActiveRecord::Base  endSunday, November 13, 11
Where are the methods?Sunday, November 13, 11
$ rails consoleSunday, November 13, 11
> Message.newSunday, November 13, 11
ActiveRecord::StatementInvalid:  Could not find table messages  from /Users/o_o/.rvm/gems/ruby-1.9.2-p0@rails3/  gems/activ...
every model has a corresponding          database tableSunday, November 13, 11
$ rails dbSunday, November 13, 11
PRAGMA table_info(messages);Sunday, November 13, 11
nothing!Sunday, November 13, 11
.quit;Sunday, November 13, 11
$ rake db:migrateSunday, November 13, 11
$ rails dbSunday, November 13, 11
PRAGMA table_info(messages);Sunday, November 13, 11
0|id|INTEGER|1||1                          1|body|text|0||0                          2|public_key|text|0||0               ...
.quitSunday, November 13, 11
$ rails consoleSunday, November 13, 11
> Message.newSunday, November 13, 11
=> #<Message id: nil, body: nil, public_key: nil,         created_at: nil, updated_at: nil>Sunday, November 13, 11
>msg = Message.new  >msg.body = 2  >msg.save  >msgSunday, November 13, 11
=> #<Message id: 1, body: 2, public_key: nil,  created_at: "2011-02-08 20:56:33", updated_at:  "2011-02-08 20:56:33">Sunda...
> msg.body.classSunday, November 13, 11
=> FixnumSunday, November 13, 11
> Message.find(1)Sunday, November 13, 11
=> #<Message id: 1, body: "2", public_key: nil,  created_at: "2011-02-08 20:56:33", updated_at:  "2011-02-08 20:56:33">Sun...
> Message.find(1).body.classSunday, November 13, 11
=> StringSunday, November 13, 11
> Message.find(1).updated_at.classSunday, November 13, 11
=> ActiveSupport::TimeWithZoneSunday, November 13, 11
One more time..Sunday, November 13, 11
$ rails consoleSunday, November 13, 11
> Message.create!Sunday, November 13, 11
SQL (0.3ms) SELECT name  FROM sqlite_master  WHERE type = table AND NOT name = sqlite_sequence   AREL (0.5ms) INSERT INTO ...
> exitSunday, November 13, 11
Active Record FeaturesSunday, November 13, 11
validations       http://apidock.com/rails/v3.1.0/ActiveModel/ValidationsSunday, November 13, 11
before_save         http://apidock.com/rails/v3.1.0/ActiveRecord/CallbacksSunday, November 13, 11
scopes   http://apidock.com/rails/v3.1.0/ActiveRecord/NamedScope/                      ClassMethods/scopeSunday, November ...
SQL INJECTIONSunday, November 13, 11
SAFE FROM SQL INJECTION  class User < ActiveRecord::Base     def self.authenticate_unsafely(user_name, password)      find(...
QUESTIONS?Sunday, November 13, 11
Upcoming SlideShare
Loading in...5
×

Active Record Introduction - 3

1,144

Published on

Active Record

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
1,144
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Active Record Introduction - 3"

  1. 1. ACTIVERECORD Rails 3Sunday, November 13, 11
  2. 2. MVCSunday, November 13, 11
  3. 3. MODEL CLASS in app/models/person.rb class Message < ActiveRecord::Base endSunday, November 13, 11
  4. 4. EVERY MODEL HAS A CORRESPONDING DATABASE TABLESunday, November 13, 11
  5. 5. MVCSunday, November 13, 11
  6. 6. HOW DID WE GET A DATABASE TABLE?Sunday, November 13, 11
  7. 7. RUBY-DRIVEN SCHEMA • scaffold generates db/migrate/20110131021702_create_people.rb • rake db:migrate runs the migration, creates the table •Sunday, November 13, 11
  8. 8. DEMOSunday, November 13, 11
  9. 9. $ rails new ar_exSunday, November 13, 11
  10. 10. $ rails -vSunday, November 13, 11
  11. 11. Edit GemfileSunday, November 13, 11
  12. 12. source http://rubygems.org gem ‘rails’ gem ‘rspec-rails’ gem ‘sqlite3’Sunday, November 13, 11
  13. 13. $ bundleSunday, November 13, 11
  14. 14. $ rails g rspec:installSunday, November 13, 11
  15. 15. $ rails g model Message body:text public_key:textSunday, November 13, 11
  16. 16. invoke rspecSunday, November 13, 11
  17. 17. create spec/models/ message_spec.rbSunday, November 13, 11
  18. 18. require spec_helper describe Message do pending "a class in itself" endSunday, November 13, 11
  19. 19. invoke active_recordSunday, November 13, 11
  20. 20. create db/migrate/ 20110208201036_create_messages.rbSunday, November 13, 11
  21. 21. class CreateMessages < ActiveRecord::Migration def self.change # rake db:migrate end def self.up # rake db:migrate end def self.down # rake db:migrate:down VERSION=file_name # rake db:migrate:redo VERSION=file_name end endSunday, November 13, 11
  22. 22. create_table :messages do |t| t.text :body t.text :public_key t.timestamps end rails g model Message body:text public_key:textSunday, November 13, 11
  23. 23. drop_table :messagesSunday, November 13, 11
  24. 24. http://apidock.com/rails/ActiveRecord/ ConnectionAdapters/SchemaStatementsSunday, November 13, 11
  25. 25. create app/models/message.rbSunday, November 13, 11
  26. 26. class Message < ActiveRecord::Base endSunday, November 13, 11
  27. 27. Where are the methods?Sunday, November 13, 11
  28. 28. $ rails consoleSunday, November 13, 11
  29. 29. > Message.newSunday, November 13, 11
  30. 30. ActiveRecord::StatementInvalid: Could not find table messages from /Users/o_o/.rvm/gems/ruby-1.9.2-p0@rails3/ gems/activerecord-3.0.3/lib/active_record/ connection_adapters/sqlite_adapter.rb:295:in `table_structureSunday, November 13, 11
  31. 31. every model has a corresponding database tableSunday, November 13, 11
  32. 32. $ rails dbSunday, November 13, 11
  33. 33. PRAGMA table_info(messages);Sunday, November 13, 11
  34. 34. nothing!Sunday, November 13, 11
  35. 35. .quit;Sunday, November 13, 11
  36. 36. $ rake db:migrateSunday, November 13, 11
  37. 37. $ rails dbSunday, November 13, 11
  38. 38. PRAGMA table_info(messages);Sunday, November 13, 11
  39. 39. 0|id|INTEGER|1||1 1|body|text|0||0 2|public_key|text|0||0 3|created_at|datetime|0||0 4|updated_at|datetime|0||0Sunday, November 13, 11
  40. 40. .quitSunday, November 13, 11
  41. 41. $ rails consoleSunday, November 13, 11
  42. 42. > Message.newSunday, November 13, 11
  43. 43. => #<Message id: nil, body: nil, public_key: nil, created_at: nil, updated_at: nil>Sunday, November 13, 11
  44. 44. >msg = Message.new >msg.body = 2 >msg.save >msgSunday, November 13, 11
  45. 45. => #<Message id: 1, body: 2, public_key: nil, created_at: "2011-02-08 20:56:33", updated_at: "2011-02-08 20:56:33">Sunday, November 13, 11
  46. 46. > msg.body.classSunday, November 13, 11
  47. 47. => FixnumSunday, November 13, 11
  48. 48. > Message.find(1)Sunday, November 13, 11
  49. 49. => #<Message id: 1, body: "2", public_key: nil, created_at: "2011-02-08 20:56:33", updated_at: "2011-02-08 20:56:33">Sunday, November 13, 11
  50. 50. > Message.find(1).body.classSunday, November 13, 11
  51. 51. => StringSunday, November 13, 11
  52. 52. > Message.find(1).updated_at.classSunday, November 13, 11
  53. 53. => ActiveSupport::TimeWithZoneSunday, November 13, 11
  54. 54. One more time..Sunday, November 13, 11
  55. 55. $ rails consoleSunday, November 13, 11
  56. 56. > Message.create!Sunday, November 13, 11
  57. 57. SQL (0.3ms) SELECT name FROM sqlite_master WHERE type = table AND NOT name = sqlite_sequence AREL (0.5ms) INSERT INTO "messages" ("body", "public_key", "created_at", "updated_at") VALUES (NULL, NULL, 2011-02-09 01:55:09.111181, 2011-02-09 01:55:09.111181)Sunday, November 13, 11
  58. 58. > exitSunday, November 13, 11
  59. 59. Active Record FeaturesSunday, November 13, 11
  60. 60. validations http://apidock.com/rails/v3.1.0/ActiveModel/ValidationsSunday, November 13, 11
  61. 61. before_save http://apidock.com/rails/v3.1.0/ActiveRecord/CallbacksSunday, November 13, 11
  62. 62. scopes http://apidock.com/rails/v3.1.0/ActiveRecord/NamedScope/ ClassMethods/scopeSunday, November 13, 11
  63. 63. SQL INJECTIONSunday, November 13, 11
  64. 64. 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 }) endSunday, November 13, 11
  65. 65. QUESTIONS?Sunday, November 13, 11
  1. A particular slide catching your eye?

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

×