標籤:des style blog http io color os 使用 sp
本章節展開對model的介紹:包括查增刪改操作。緊接著上面一節《[ruby on rails] 跟我學之HelloWorld》
建立模型使用命令建立模型
建立表post,預設內建兩欄位 title :string, content:text , 在模型裡面按照約定使用單數post而不是複數posts
cd blograils g model post title:string content:text
輸出:
invoke active_record create db/migrate/20141203105453_create_posts.rb create app/models/post.rb invoke test_unit create test/models/post_test.rb create test/fixtures/posts.yml
db/migrate/20141203105453_create_posts.rb檔案內容如下:
class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.text :context t.timestamps end endend
以上只是產生了遷移檔案,而沒有正式產生資料庫。 需要運行以下指令來產生資料庫。
rake db:migrate
相當於django的syncdb。 輸出如下:
== 20141203105453 CreatePosts: migrating ======================================-- create_table(:posts) -> 0.0021s== 20141203105453 CreatePosts: migrated (0.0029s) =============================
從控制台訪問模型
進入控制台,類似於django的manage shell指令,ror使用以下指令:
rails console
簡寫為 rails c
新增記錄:
模型類.new -> 模型執行個體變數.save
2.1.5 :001 > p = Post.new(:title => "My First Post", :context=>"this is my first post") => #<Post id: nil, title: "My First Post", context: "this is my first post", created_at: nil, updated_at: nil>2.1.5 :002 > p.save() (0.5ms) begin transaction SQL (0.7ms) INSERT INTO "posts" ("context", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["context", "this is my first post"], ["created_at", "2014-12-03 11:44:50.954572"], ["title", "My First Post"], ["updated_at", "2014-12-03 11:44:50.954572"]] (23.0ms) commit transaction => true
模型類.create
2.1.5 :003 > Post.create(:title => "create test", :context=>"test of create") (0.2ms) begin transaction SQL (0.4ms) INSERT INTO "posts" ("context", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["context", "test of create"], ["created_at", "2014-12-03 11:48:08.779270"], ["title", "create test"], ["updated_at", "2014-12-03 11:48:08.779270"]] (21.9ms) commit transaction => #<Post id: 2, title: "create test", context: "test of create", created_at: "2014-12-03 11:48:08", updated_at: "2014-12-03 11:48:08">
查詢記錄
模型類.all
2.1.5 :004 > posts = Post.all Post Load (0.6ms) SELECT "posts".* FROM "posts" => #<ActiveRecord::Relation [#<Post id: 1, title: "My First Post", context: "this is my first post", created_at: "2014-12-03 11:44:50", updated_at: "2014-12-03 11:44:50">, #<Post id: 2, title: "create test", context: "test of create", created_at: "2014-12-03 11:48:08", updated_at: "2014-12-03 11:48:08">]>
模型類.find(id)
2.1.5 :005 > post = Post.find(2) Post Load (0.6ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]] => #<Post id: 2, title: "create test", context: "test of create", created_at: "2014-12-03 11:48:08", updated_at: "2014-12-03 11:48:08">
可以用[執行個體變數].[成員變數]的方式訪問資料,如下:
2.1.5 :006 > post.title => "create test"2.1.5 :007 > post.context => "test of create"
更新記錄
模型執行個體變數.update -> 模型執行個體變數.save()
2.1.5 :008 > post.title = "test update" => "test update"2.1.5 :009 > post.save() (0.4ms) begin transaction SQL (1.2ms) UPDATE "posts" SET "title" = ?, "updated_at" = ? WHERE "posts"."id" = 2 [["title", "test update"], ["updated_at", "2014-12-03 11:57:08.964494"]] (10.0ms) commit transaction => true
模型執行個體變數.update_attribute(field,value)
2.1.5 :010 > post.update_attribute(:context,"test operation of update_attribute") (0.4ms) begin transaction SQL (1.4ms) UPDATE "posts" SET "context" = ?, "updated_at" = ? WHERE "posts"."id" = 2 [["context", "test operation of update_attribute"], ["updated_at", "2014-12-03 12:01:12.051869"]] (32.3ms) commit transaction => true
模型執行個體變數.update_attributes(hash)
2.1.5 :013 > post.update_attributes(:title=>"test update_attribute 2", :context =>"content for test of update_attribute 2") (1.4ms) begin transaction SQL (1.2ms) UPDATE "posts" SET "context" = ?, "title" = ?, "updated_at" = ? WHERE "posts"."id" = 2 [["context", "content for test of update_attribute 2"], ["title", "test update_attribute 2"], ["updated_at", "2014-12-03 12:05:16.878764"]] (26.1ms) commit transaction => true
刪除記錄
模型執行個體變數.destroy
2.1.5 :016 > post.destroy (0.3ms) begin transaction SQL (1.3ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 2]] (23.6ms) commit transaction => #<Post id: 2, title: "test update_attribute 2", context: "content for test of update_attribute 2", created_at: "2014-12-03 11:48:08", updated_at: "2014-12-03 12:05:16">
轉載請註明本文來自:http://www.cnblogs.com/Tommy-Yu/p/4141122.html,謝謝!
[ruby on rails] 跟我學之基於rails console的查增刪改操作