RoR开发-使用脚本产生model-表

使用RoR 脚本Generator产生controller test

脚本如下:

>script/generate controller test

产生如下文件:
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/test
      exists  test/functional/
      exists  test/unit/helpers/
      create  app/controllers/test_controller.rb
      create  test/functional/test_controller_test.rb
      create  app/helpers/test_helper.rb
   identical  test/unit/helpers/test_helper_test.rb

要想建立数据库model,并产生表,可以使用如下脚本命令:

如:建立数据库表notices(对应着类:Notice,这其中涉及到rails的名字自动转变)

>script/generate model notice

产生如下文件:
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/notice.rb
      create  test/unit/notice_test.rb
      create  test/fixtures/notices.yml
      exists  db/migrate
      create  db/migrate/20091010031714_create_notices.rb

其中:

create  app/models/notice.rb显示产生notice.rb文件,类名是Notice,文件内容如下:

class Notice < ActiveRecord::Base
end

create  db/migrate/20091010031714_create_notices.rb显示在db目录的子目录migrate产生20091010031714_create_notices.rb文件,名字前的20091010031714事rails用来防止文件冲突的一种时间戳,文件内容如下:

class CreateNotices < ActiveRecord::Migration
  def self.up
    create_table :notices do |t|
     t.timestamps
    end
  end

  def self.down
    drop_table :notices
  end
end

要想在表notices中建立不同的列,修改如下

class CreateNotices < ActiveRecord::Migration
  def self.up(用来生成表)
    create_table :notices do |t|
     t.string :title(建立string类型的title列)
     t.string :content
     t.integer :order_index(建立integer类型的order_index列)
     t.integer :is_show
  
     t.timestamps(此列是rails自动生成的,会自动生成datetime类型的created_time和updated_time列,不需要时可以去掉)
    end
  end

  def self.down(用来删除表)
    drop_table :notices
  end
end

当修改后,为了在数据库真正建立表notices,可以使用如下命令

>rake db:migrate
输出:
==  CreateNotices: migrating ==================================================
-- create_table(:notices)
   -> 0.0310s
==  CreateNotices: migrated (0.0310s) =========================================

产生表notices

在生成表后,如果修改表notices中的列字段,可以使用脚本如下

>script/generate migration change_is_show_to_notice

change_is_show_to_notice名字表示修改notice表中的is_show字段

运行输出如下:
      exists  db/migrate
      create  db/migrate/20091010034904_change_is_show_to_notice.rb

20091010034904_change_is_show_to_notice.rb文件内容如下:

class ChangeIsShowToNotice < ActiveRecord::Migration
  def self.up
    
  end

  def self.down
  end
end

修改:

class ChangeIsShowToNotice < ActiveRecord::Migration
  def self.up
     change_column(:notices, :is_show, :boolean)(改变is_show类型为boolean型)
     change_column_default(:notices, :is_show, 0)(改变is_show 的默认值为0)
     change_column_default(:notices, :order_index, -1)
  end

  def self.down
  end
end

把修改持久化到表,运行如下命令;

>rake db:migrate
(in D:/rwork/ShellSNS)
==  ChangeIsShowToNotice: migrating ===========================================
-- change_column(:notices, :is_show, :boolean)
   -> 0.1400s
-- change_column_default(:notices, :is_show, 0)
   -> 0.0160s
-- change_column_default(:notices, :order_index, -1)
   -> 0.0160s
==  ChangeIsShowToNotice: migrated (0.1720s) ==================================

基于SpringBoot网上超市,系统包含两种角色:用户、管理员,系统分为前台和后台两大模块,主要功能如下: 1 管理员功能实现 商品信息管理 管理员可以通过提交商品名称查询商品,并查看该商品的用户评论信息。 用户管理 管理员通过提交用户名来获取用户资料,对有异常情况的用户信息进行修改,并可以详细查看用户资料。 商品评价管理 管理员审核用户对商品的评价,经过审核的评价才会显示,并可以统计商品评价信息。 已支付订单 管理员查看已支付的订单,并逐个进行订单发货。 2 用户功能实现 商品信息 用户可以收藏、立即购买商品,或对商品进行评价,同时将商品添加到购物车。 购物车 用户可以直接下单购买购物车中的商品,或删除购物车中的商品。 确认下单 用户选择地址,查看支付金额信息,以确认订单之前的所有细节。 已支付订单 用户查看已支付的订单,若对购买商品产生后悔,可以申请退款。 二、项目技术 开发语言:Java 数据库:MySQL 项目管理工具:Maven Web应用服务器:Tomcat 前端技术:Vue、 后端技术:SpringBoot框架 三、运行环境 操作系统:Windows、macOS都可以 JDK版本:JDK1.8以上版本都可以 开发工具:IDEA、Ecplise都可以 数据库: MySQL 5.7/8.0版本均可 Tomcat:7.x、8.x、9.x版本均可 Maven:任意版本都可以
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值