Active Record Introduction - 3
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,492
On Slideshare
1,488
From Embeds
4
Number of Embeds
1

Actions

Shares
Downloads
14
Comments
0
Likes
0

Embeds 4

http://cemsfa.wordpress.com 4

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ACTIVERECORD Rails 3Sunday, November 13, 11
  • 2. MVCSunday, November 13, 11
  • 3. MODEL CLASS in app/models/person.rb class Message < ActiveRecord::Base endSunday, November 13, 11
  • 4. EVERY MODEL HAS A CORRESPONDING DATABASE TABLESunday, November 13, 11
  • 5. MVCSunday, November 13, 11
  • 6. HOW DID WE GET A DATABASE TABLE?Sunday, November 13, 11
  • 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. DEMOSunday, November 13, 11
  • 9. $ rails new ar_exSunday, November 13, 11
  • 10. $ rails -vSunday, November 13, 11
  • 11. Edit GemfileSunday, November 13, 11
  • 12. source http://rubygems.org gem ‘rails’ gem ‘rspec-rails’ gem ‘sqlite3’Sunday, November 13, 11
  • 13. $ bundleSunday, November 13, 11
  • 14. $ rails g rspec:installSunday, November 13, 11
  • 15. $ rails g model Message body:text public_key:textSunday, November 13, 11
  • 16. invoke rspecSunday, November 13, 11
  • 17. create spec/models/ message_spec.rbSunday, November 13, 11
  • 18. require spec_helper describe Message do pending "a class in itself" endSunday, November 13, 11
  • 19. invoke active_recordSunday, November 13, 11
  • 20. create db/migrate/ 20110208201036_create_messages.rbSunday, November 13, 11
  • 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. 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. drop_table :messagesSunday, November 13, 11
  • 24. http://apidock.com/rails/ActiveRecord/ ConnectionAdapters/SchemaStatementsSunday, November 13, 11
  • 25. create app/models/message.rbSunday, November 13, 11
  • 26. class Message < ActiveRecord::Base endSunday, November 13, 11
  • 27. Where are the methods?Sunday, November 13, 11
  • 28. $ rails consoleSunday, November 13, 11
  • 29. > Message.newSunday, November 13, 11
  • 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. every model has a corresponding database tableSunday, November 13, 11
  • 32. $ rails dbSunday, November 13, 11
  • 33. PRAGMA table_info(messages);Sunday, November 13, 11
  • 34. nothing!Sunday, November 13, 11
  • 35. .quit;Sunday, November 13, 11
  • 36. $ rake db:migrateSunday, November 13, 11
  • 37. $ rails dbSunday, November 13, 11
  • 38. PRAGMA table_info(messages);Sunday, November 13, 11
  • 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. .quitSunday, November 13, 11
  • 41. $ rails consoleSunday, November 13, 11
  • 42. > Message.newSunday, November 13, 11
  • 43. => #<Message id: nil, body: nil, public_key: nil, created_at: nil, updated_at: nil>Sunday, November 13, 11
  • 44. >msg = Message.new >msg.body = 2 >msg.save >msgSunday, November 13, 11
  • 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. > msg.body.classSunday, November 13, 11
  • 47. => FixnumSunday, November 13, 11
  • 48. > Message.find(1)Sunday, November 13, 11
  • 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. > Message.find(1).body.classSunday, November 13, 11
  • 51. => StringSunday, November 13, 11
  • 52. > Message.find(1).updated_at.classSunday, November 13, 11
  • 53. => ActiveSupport::TimeWithZoneSunday, November 13, 11
  • 54. One more time..Sunday, November 13, 11
  • 55. $ rails consoleSunday, November 13, 11
  • 56. > Message.create!Sunday, November 13, 11
  • 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. > exitSunday, November 13, 11
  • 59. Active Record FeaturesSunday, November 13, 11
  • 60. validations http://apidock.com/rails/v3.1.0/ActiveModel/ValidationsSunday, November 13, 11
  • 61. before_save http://apidock.com/rails/v3.1.0/ActiveRecord/CallbacksSunday, November 13, 11
  • 62. scopes http://apidock.com/rails/v3.1.0/ActiveRecord/NamedScope/ ClassMethods/scopeSunday, November 13, 11
  • 63. SQL INJECTIONSunday, November 13, 11
  • 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. QUESTIONS?Sunday, November 13, 11