- 論壇徽章:
- 0
|
##在已有數(shù)據(jù)模型的基礎上添加字段 新建遷移文件
rails generate migration add_quantity_to_line_items quantity:integer
rails generate migration remove_quantity_from_line_items quantity:integer
##創(chuàng)建控制器
rails generate controller 單數(shù)形式的類名 方法名
##Rails腳手架創(chuàng)建
rails generate scaffold line_item(類名) product:integer(屬性及屬性的類型)
##查詢安裝的所有Rails版本
gem list --local rails
##使用指定的Rails版本創(chuàng)建工程
rails _3.2.9_ new app_name
##創(chuàng)建模型文件
rails generate model Article title:string text:text
##創(chuàng)建JoinTable,創(chuàng)建聯(lián)合數(shù)據(jù)表
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 類型字段的精度,即數(shù)字的位數(shù);
scale:設置 decimal 類型字段小數(shù)點后的數(shù)字位數(shù);
polymorphic:為 belongs_to 關聯(lián)添加 type 字段;
null:是否允許該字段的值為 NULL;
例如,執(zhí)行下面的命令:
$ 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 |
|