ActiveRecord::Base    Cur$s	  Schofield
$ rails new ar_ex
$ rails -v 3.0.3
Edit Gemfile
source http://rubygems.orggem ‘rails’,‘3.0.3’gem ‘rspec-rails’gem ‘sqlite3-ruby’, :require => ‘sqlite’
$ bundle
$ rails g rspec:install
$ rails g model Messagebody:text public_key:text
invoke   rspec
create   spec/models/   message_spec.rb
require spec_helperdescribe Message do  pending "a class in itself"end
invoke active_record
create db/migrate/20110208201036_create_messages.rb
class CreateMessages < ActiveRecord::Migration  def self.up     # rake db:migrate  end  def self.down     # rake db:migrat...
create_table :messages do |t|               t.text :body               t.text :public_key                t.timestamps     ...
drop_table :messages
http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements
create   app/models/message.rb
class Message < ActiveRecord::Baseend
Where are the methods?
$ rails console
> Message.new
ActiveRecord::StatementInvalid:Could not find table messagesfrom /Users/o_o/.rvm/gems/ruby-1.9.2-p0@rails3/gems/activereco...
every model has a corresponding        database table
$ rails db
PRAGMA table_info(messages);
nothing!
.quit;
$ rake db:migrate
$ rails db
PRAGMA table_info(messages);
0|id|INTEGER|1||11|body|text|0||02|public_key|text|0||03|created_at|datetime|0||04|updated_at|datetime|0||0
.quit
$ rails console
> Message.new
=> #<Message id: nil, body: nil, public_key:nil, created_at: nil, updated_at: nil>
>msg = Message.new>msg.body = 2>msg.save>msg
=> #<Message id: 1, body: 2, public_key: nil,created_at: "2011-02-08 20:56:33", updated_at:"2011-02-08 20:56:33">
>   msg.body.class
=> Fixnum
> Message.find(1)
=> #<Message id: 1, body: "2", public_key:nil, created_at: "2011-02-08 20:56:33",updated_at: "2011-02-08 20:56:33">
> Message.find(1).body.class
=> String
> Message.find(1).updated_at.class
=> ActiveSupport::TimeWithZone
With Great Power Comes With     Great Responsibility
One more time..
$ rails console
> ActiveRecord::Base.logger=Logger.new($stdout)
> Message.new.save
SQL (0.3ms)   SELECT name FROM sqlite_master WHERE type = table AND NOT name =sqlite_sequence  AREL (0.5ms) INSERT INTO "m...
> exit
Active Record Features
validationshttp://apidock.com/rails/v3.0.0/ActiveModel/Validations
before_savehttp://apidock.com/rails/v3.0.0/ActiveRecord/Callbacks
scopeshttp://apidock.com/rails/v3.0.0/ActiveRecord/NamedScope/                   ClassMethods/scope
Understanding is free withpractice - Without practiceunderstanding cannot stand.
Upcoming SlideShare
Loading in...5
×

Rails 3 ActiveRecord

1,621

Published on

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

No notes for slide

Rails 3 ActiveRecord

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

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

×