Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- blog$ rails g model post title:string content:text
- invoke active_record
- create db/migrate/20120505085607_create_posts.rb
- create app/models/post.rb
- invoke test_unit
- create test/unit/post_test.rb
- create test/fixtures/posts.yml
- blog$ rake db:migrate
- == CreatePosts: migrating ====================================================
- -- create_table(:posts)
- -> 0.0009s
- == CreatePosts: migrated (0.0011s) ===========================================
- blog$ rails c
- Loading development environment (Rails 3.2.3)
- irb(main):001:0> post = Post.new(:title => "Fisrt Post", :content=>"Content for first post")
- => #<Post id: nil, title: "Fisrt Post", content: "Content for first post", created_at: nil, updated_at: nil>
- irb(main):002:0> post.save
- (0.1ms) begin transaction
- SQL (47.9ms) INSERT INTO "posts" ("content", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["content", "Content for first post"], ["created_at", Sat, 05 May 2012 09:09:01 UTC +00:00], ["title", "Fisrt Post"], ["updated_at", Sat, 05 May 2012 09:09:01 UTC +00:00]]
- (8.1ms) commit transaction
- => true
- irb(main):003:0> post
- => #<Post id: 1, title: "Fisrt Post", content: "Content for first post", created_at: "2012-05-05 09:09:01", updated_at: "2012-05-05 09:09:01">
- irb(main):004:0> Post.create(:title => "Second Post", :content => "Content for second post")
- (0.1ms) begin transaction
- SQL (0.4ms) INSERT INTO "posts" ("content", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["content", "Content for second post"], ["created_at", Sat, 05 May 2012 09:11:09 UTC +00:00], ["title", "Second Post"], ["updated_at", Sat, 05 May 2012 09:11:09 UTC +00:00]]
- (7.7ms) commit transaction
- => #<Post id: 2, title: "Second Post", content: "Content for second post", created_at: "2012-05-05 09:11:09", updated_at: "2012-05-05 09:11:09">
- irb(main):005:0> posts = Post.all
- Post Load (0.2ms) SELECT "posts".* FROM "posts"
- => [#<Post id: 1, title: "Fisrt Post", content: "Content for first post", created_at: "2012-05-05 09:09:01", updated_at: "2012-05-05 09:09:01">, #<Post id: 2, title: "Second Post", content: "Content for second post", created_at: "2012-05-05 09:11:09", updated_at: "2012-05-05 09:11:09">]
- irb(main):006:0> post = Post.find(2)
- Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]]
- => #<Post id: 2, title: "Second Post", content: "Content for second post", created_at: "2012-05-05 09:11:09", updated_at: "2012-05-05 09:11:09">
- irb(main):007:0> post.title
- => "Second Post"
- irb(main):008:0> post.content
- => "Content for second post"
- irb(main):009:0> post.title = "Our 2nd Post"
- => "Our 2nd Post"
- irb(main):010:0> post.save
- (0.1ms) begin transaction
- (0.2ms) UPDATE "posts" SET "title" = 'Our 2nd Post', "updated_at" = '2012-05-05 09:12:58.570208' WHERE "posts"."id" = 2
- (8.7ms) commit transaction
- => true
- irb(main):011:0> post.update_attribute(:content, "Content for our 2nd post")
- (0.1ms) begin transaction
- (0.2ms) UPDATE "posts" SET "content" = 'Content for our 2nd post', "updated_at" = '2012-05-05 09:14:20.311204' WHERE "posts"."id" = 2
- (7.5ms) commit transaction
- => true
- irb(main):013:0> post.update_attributes(:title => "Second Post", :content => "Content for second post")
- (0.1ms) begin transaction
- (0.3ms) UPDATE "posts" SET "title" = 'Second Post', "content" = 'Content for second post', "updated_at" = '2012-05-05 09:16:53.926752' WHERE "posts"."id" = 2
- (7.9ms) commit transaction
- => true
- irb(main):014:0> post
- => #<Post id: 2, title: "Second Post", content: "Content for second post", created_at: "2012-05-05 09:11:09", updated_at: "2012-05-05 09:16:53">
- irb(main):015:0> post.destroy
- (0.1ms) begin transaction
- SQL (0.5ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 2]]
- (7.8ms) commit transaction
- => #<Post id: 2, title: "Second Post", content: "Content for second post", created_at: "2012-05-05 09:11:09", updated_at: "2012-05-05 09:16:53">
- irb(main):016:0> Post.find(2)
- Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]]
- ActiveRecord::RecordNotFound: Couldn't find Post with id=2
- from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:340:in `find_one'
- from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:311:in `find_with_ids'
- from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:107:in `find'
- from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/querying.rb:5:in `find'
- from (irb):16
- from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
- from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
- from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
- from script/rails:6:in `require'
- from script/rails:6:in `<main>'
Advertisement
Add Comment
Please, Sign In to add comment