关闭

create the model and table

标签: tablemigrationactiverecordgeneratorrailsstring
273人阅读 评论(0) 收藏 举报
分类:

 work>ruby script/generate model product

the generator creates a bunch of files. The two we're interested in are the model itself product.rb, and the migration 001_create_products.rb.

In this file, there were two ruby methods.

class CretaeProducts < ActiveRecord::Migration

def self.up

  create_table :products do |t|

    t.column :title,  :string

    t.column :description, :text

  end

def self.down

  drop_table :products

end

end

get rails to apply the migration to the database.

work> rake db:migrate

if you want to roll back ,you could use

work> rake db:migrate VERSION=0

and the products table will gone.

call the rake db:migrate will re-create it.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23214次
    • 积分:444
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:10篇
    • 译文:0篇
    • 评论:0条
    文章分类