yeyicheng

Untitled

May 5th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. blog$ rails g model post title:string content:text
  2.       invoke  active_record
  3.       create    db/migrate/20120505085607_create_posts.rb
  4.       create    app/models/post.rb
  5.       invoke    test_unit
  6.       create      test/unit/post_test.rb
  7.       create      test/fixtures/posts.yml
  8.  
  9. blog$ rake db:migrate
  10. ==  CreatePosts: migrating ====================================================
  11. -- create_table(:posts)
  12.    -> 0.0009s
  13. ==  CreatePosts: migrated (0.0011s) ===========================================
  14.  
  15. blog$ rails c
  16. Loading development environment (Rails 3.2.3)
  17. irb(main):001:0> post = Post.new(:title => "Fisrt Post", :content=>"Content for first post")
  18. => #<Post id: nil, title: "Fisrt Post", content: "Content for first post", created_at: nil, updated_at: nil>
  19.  
  20. irb(main):002:0> post.save
  21.    (0.1ms)  begin transaction
  22.   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]]
  23.    (8.1ms)  commit transaction
  24. => true
  25.  
  26. irb(main):003:0> post
  27. => #<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">
  28.  
  29. irb(main):004:0> Post.create(:title => "Second Post", :content => "Content for second post")
  30.    (0.1ms)  begin transaction
  31.   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]]
  32.    (7.7ms)  commit transaction
  33. => #<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">
  34.  
  35. irb(main):005:0> posts = Post.all
  36.   Post Load (0.2ms)  SELECT "posts".* FROM "posts"
  37. => [#<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">]
  38.  
  39. irb(main):006:0> post = Post.find(2)
  40.   Post Load (0.3ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 2]]
  41. => #<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">
  42.  
  43. irb(main):007:0> post.title
  44. => "Second Post"
  45.  
  46. irb(main):008:0> post.content
  47. => "Content for second post"
  48.  
  49. irb(main):009:0> post.title = "Our 2nd Post"
  50. => "Our 2nd Post"
  51.  
  52. irb(main):010:0> post.save
  53.    (0.1ms)  begin transaction
  54.    (0.2ms)  UPDATE "posts" SET "title" = 'Our 2nd Post', "updated_at" = '2012-05-05 09:12:58.570208' WHERE "posts"."id" = 2
  55.    (8.7ms)  commit transaction
  56. => true
  57.  
  58. irb(main):011:0> post.update_attribute(:content, "Content for our 2nd post")
  59.    (0.1ms)  begin transaction
  60.    (0.2ms)  UPDATE "posts" SET "content" = 'Content for our 2nd post', "updated_at" = '2012-05-05 09:14:20.311204' WHERE "posts"."id" = 2
  61.    (7.5ms)  commit transaction
  62. => true
  63.  
  64. irb(main):013:0> post.update_attributes(:title => "Second Post", :content => "Content for second post")
  65.    (0.1ms)  begin transaction
  66.    (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
  67.    (7.9ms)  commit transaction
  68. => true
  69.  
  70. irb(main):014:0> post
  71. => #<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">
  72.  
  73. irb(main):015:0> post.destroy
  74.    (0.1ms)  begin transaction
  75.   SQL (0.5ms)  DELETE FROM "posts" WHERE "posts"."id" = ?  [["id", 2]]
  76.    (7.8ms)  commit transaction
  77. => #<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">
  78.  
  79. irb(main):016:0> Post.find(2)
  80.   Post Load (0.1ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 2]]
  81. ActiveRecord::RecordNotFound: Couldn't find Post with id=2
  82.     from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:340:in `find_one'
  83.     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'
  84.     from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/relation/finder_methods.rb:107:in `find'
  85.     from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.3/lib/active_record/querying.rb:5:in `find'
  86.     from (irb):16
  87.     from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
  88.     from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
  89.     from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
  90.     from script/rails:6:in `require'
  91.     from script/rails:6:in `<main>'
Advertisement
Add Comment
Please, Sign In to add comment