本章节展开对model的介绍:包括查增删改操作。紧接着上面一节《[ruby on rails] 跟我学之HelloWorld

创建模型

使用命令创建模型

创建表post,默认自带两栏位 title :string, content:text ,  在模型里面按照约定使用单数post而不是复数posts

cd blog
rails 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
end
end

以上只是生成了迁移文件,而没有正式生成数据库。 需要运行以下指令来生成数据库。

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] 跟我学之(3)基于rails console的查增删改操作的更多相关文章

  1. 一步一步学Linq to sql(三):增删改

    示例数据库 字段名 字段类型 允许空 字段说明 ID uniqueidentifier 表主键字段 UserName varchar(50) 留言用户名 PostTime datetime 留言时间 ...

  2. [ruby on rails] 跟我学之(9)删除数据

    首先需要在index页加个删除链接,并提供一个删除的确认,用户确认删除时,直接删除数据. 修改views 修改 app/views/posts/index.html.erb,如下: <h1> ...

  3. [ruby on rails] 跟我学之(8)修改数据

    修改views 修改index视图(app/views/posts/index.html.erb),添加编辑链接,如下: <h1>Our blogs</h1> <% @p ...

  4. [ruby on rails] 跟我学之(7)创建数据

    通过form来创建数据,本章节将会涉及内容:创建form,用户重导向,渲染views 和 flash消息. 1. views初步 编辑 app/views/posts/index.html.erb这个 ...

  5. [ruby on rails] 跟我学之(6)显示指定数据

    根据<[ruby on rails] 跟我学之路由映射>,我们知道,可以访问 GET    /posts/:id(.:format) 来显示具体的对象. 1. 修改action 修改 ap ...

  6. [ruby on rails] 跟我学之(5)显示所有数据

    之前的index页,显示的是hello world,现在将其修改为显示我们在rails console里面录入的数据. 1. 修改action 如之前的章节<[ruby on rails] 跟我 ...

  7. [ruby on rails] 跟我学之(4)路由映射

    前面<[ruby on rails] 跟我学之Hello World>提到,路由对应的文件是 config/routes.rb 实际上我们只是添加了一句代码: resources :pos ...

  8. 跟我一起学extjs5(18--模块的新增、改动、删除操作)

    跟我一起学extjs5(18--模块的新增.改动.删除操作)         上节在Grid展示时做了一个金额单位能够手工选择的功能,假设你要增加其它功能.也仅仅要依照这个模式来操作即可了,比方说你想 ...

  9. [从源码学设计]蚂蚁金服SOFARegistry之网络封装和操作

    [从源码学设计]蚂蚁金服SOFARegistry之网络封装和操作 目录 [从源码学设计]蚂蚁金服SOFARegistry之网络封装和操作 0x00 摘要 0x01 业务领域 1.1 SOFARegis ...

随机推荐

  1. WCF入门(7)

    前言 前段时间忙着驾照科目二的考试,都没有机会碰自己的电脑.说起来也是第一次参加这么没信心的考试,不过好在过了. 再打个广告吧,昨天终于把下载的WCF视频全部传到了QQ群共享里面,群号37819043 ...

  2. SQL删除重复的记录(只保留一条)

    首先新建表: --创建示例表 CREATE TABLE t ( id ,) PRIMARY KEY, a ), b ) ) --插入数据 INSERT INTO t SELECT 'aa','bb' ...

  3. α发布后的感想(组长作业)

    今天α发布后,组长作业,谈谈心得体会,谈谈哪些做的好的地方,哪些做的不好.耐撕团队组长因有事缺席,耐撕团队的α发布由齐同学来主持,所以这个作业由齐同学代理. 先谈谈耐撕团队在α发布会中齐同学认为做的好 ...

  4. NABCD模型进行竞争性需求分析

    确定项目:教室管理系统 负责人:李凤娇,高德建 选择比努力更重要.一个项目成功自然离不开组员们的努力.但是,光努力是不够的.还需要用户有需求,能快速实现. 这些东西,看似很虚,却能让我们少走不少弯路. ...

  5. 转: maven进阶:一个多模块项目

    一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...

  6. 缓存插件 EHCache

    EHCache是来自sourceforge(http://ehcache.sourceforge.net/)的开源项目,也是纯Java实现的简单.快速的Cache组件. 下载jar包 Ehcache ...

  7. ThinkPHP多表联合查询的常用方法

    1.原生查询示例: $Model = new Model(); $sql = 'select a.id,a.title,b.content from think_test1 as a, think_t ...

  8. POJ3041Asteroids(最小点覆盖+有点小抽象)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18289   Accepted: 9968 Descri ...

  9. 教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏

    早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用C写一个功能基本齐全的俄罗斯方块的话,大 ...

  10. BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler错误的解决方法

    出现这个问题是因为我的spring3.0里的包是单独引用的,缺少了别的包 譬如Configuration problem: Unable to locate Spring NamespaceHandl ...