Your SlideShare is downloading. ×
Rails 3 ActiveRecord
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Rails 3 ActiveRecord

1,596
views

Published on

Blazing Cloud Rails class. …

Blazing Cloud Rails class.

details on the 'rails generate model' command.

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

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::Base Cur$s  Schofield
  • 2. $ rails new ar_ex
  • 3. $ rails -v 3.0.3
  • 4. Edit Gemfile
  • 5. source http://rubygems.orggem ‘rails’,‘3.0.3’gem ‘rspec-rails’gem ‘sqlite3-ruby’, :require => ‘sqlite’
  • 6. $ bundle
  • 7. $ rails g rspec:install
  • 8. $ rails g model Messagebody:text public_key:text
  • 9. invoke rspec
  • 10. create spec/models/ message_spec.rb
  • 11. require spec_helperdescribe Message do pending "a class in itself"end
  • 12. invoke active_record
  • 13. create db/migrate/20110208201036_create_messages.rb
  • 14. class CreateMessages < ActiveRecord::Migration def self.up # rake db:migrate end def self.down # rake db:migrate:down VERSION=file_name # rake db:migrate:redo VERSION=file_name endend
  • 15. create_table :messages do |t| t.text :body t.text :public_key t.timestamps endrails g model Message body:text public_key:text
  • 16. drop_table :messages
  • 17. http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements
  • 18. create app/models/message.rb
  • 19. class Message < ActiveRecord::Baseend
  • 20. Where are the methods?
  • 21. $ rails console
  • 22. > Message.new
  • 23. ActiveRecord::StatementInvalid:Could not find table messagesfrom /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_structure
  • 24. every model has a corresponding database table
  • 25. $ rails db
  • 26. PRAGMA table_info(messages);
  • 27. nothing!
  • 28. .quit;
  • 29. $ rake db:migrate
  • 30. $ rails db
  • 31. PRAGMA table_info(messages);
  • 32. 0|id|INTEGER|1||11|body|text|0||02|public_key|text|0||03|created_at|datetime|0||04|updated_at|datetime|0||0
  • 33. .quit
  • 34. $ rails console
  • 35. > Message.new
  • 36. => #<Message id: nil, body: nil, public_key:nil, created_at: nil, updated_at: nil>
  • 37. >msg = Message.new>msg.body = 2>msg.save>msg
  • 38. => #<Message id: 1, body: 2, public_key: nil,created_at: "2011-02-08 20:56:33", updated_at:"2011-02-08 20:56:33">
  • 39. > msg.body.class
  • 40. => Fixnum
  • 41. > Message.find(1)
  • 42. => #<Message id: 1, body: "2", public_key:nil, created_at: "2011-02-08 20:56:33",updated_at: "2011-02-08 20:56:33">
  • 43. > Message.find(1).body.class
  • 44. => String
  • 45. > Message.find(1).updated_at.class
  • 46. => ActiveSupport::TimeWithZone
  • 47. With Great Power Comes With Great Responsibility
  • 48. One more time..
  • 49. $ rails console
  • 50. > ActiveRecord::Base.logger=Logger.new($stdout)
  • 51. > Message.new.save
  • 52. 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-0901:55:09.111181, 2011-02-09 01:55:09.111181)
  • 53. > exit
  • 54. Active Record Features
  • 55. validationshttp://apidock.com/rails/v3.0.0/ActiveModel/Validations
  • 56. before_savehttp://apidock.com/rails/v3.0.0/ActiveRecord/Callbacks
  • 57. scopeshttp://apidock.com/rails/v3.0.0/ActiveRecord/NamedScope/ ClassMethods/scope
  • 58. Understanding is free withpractice - Without practiceunderstanding cannot stand.