- 论坛徽章:
- 0
|
##在已有数据模型的基础上添加字段 新建迁移文件
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 |
|