免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 52014 | 回复: 0

Ruby on Rails 常用命令总结 [复制链接]

论坛徽章:
0
发表于 2016-03-22 14:19 |显示全部楼层
##在已有数据模型的基础上添加字段 新建迁移文件
     rails generate migration add_quantity_to_line_items quantity:integer
     rails generate migration remove_quantity_from_line_items quantity:integer

##创建控制器   
     rails generate controller 单数形式的类名  方法名

##Rails脚手架创建
     rails generate scaffold line_item(类名) product:integer(属性及属性的类型)

##查询安装的所有Rails版本
gem list --local rails

##使用指定的Rails版本创建工程
rails _3.2.9_ new app_name

##创建模型文件
rails generate model Article title:string text:text

##创建JoinTable,创建联合数据表
rails g migration CreateJoinTableCustomerProduct customer product

生成的迁移如下:
class CreateJoinTableCustomerProduct < ActiveRecord::Migration
  def change
    create_join_table :customers, :products do |t|
      # t.index [:customer_id, :product_id]
      # t.index [:product_id, :customer_id]
    end
  end
end

##支持的类型修饰符
在字段类型后面,可以在花括号中添加选项。可用的修饰符如下:

limit:设置 string/text/binary/integer 类型字段的最大值;
precision:设置 decimal 类型字段的精度,即数字的位数;
scale:设置 decimal 类型字段小数点后的数字位数;
polymorphic:为 belongs_to 关联添加 type 字段;
null:是否允许该字段的值为 NULL;
例如,执行下面的命令:

$ rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}
生成的迁移如下:

class AddDetailsToProducts < ActiveRecord::Migration
  def change
    add_column :products, :price, :decimal, precision: 5, scale: 2
    add_reference :products, :supplier, polymorphic: true, index: true
  end
end
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP