0

例如,我有以下代碼:Rails中是否有任何方法爲遷移創建基本結構?

create_table "users", force: :cascade do |t| 
    t.string "name" 
end 

相反追加字符串自己,我想打電話給一些將建立基本遷移爲骨架像這樣的方法:

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table "users", force: :cascade do |t| 
     t.string "name" 
    end 
    end 
end 

回答

3

在Rails可以使用可用的生成器來定義大量的樣板代碼,包括遷移。

要創建(大部分)的例子,你可以使用這個命令:

bin/rails generate migration CreateUsers name:string 

,這將產生以下遷移:

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table "users" do |t| 
     t.string "name" 
    end 
    end 
end 

Rails guide on Active Record migrations描述得更詳細。請閱讀本文以及一些關於rails環境基本用法的指南。

相關問題