①rails g model 単数形
削除の場合、rails d model 単数形
↓
②マイグレーションファイルの変更
型 説明 用例
integer 数字 ユーザーのidなど
string 文字(少なめ) ユーザー名、パスワードなど
text 文字(多め) 投稿文など
boolean 真か偽か 真偽フラグ
datetime 日付と時刻 作成日時、更新日時など
例:
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.string :name
t.text :text
t.text :image
t.timestamps
end
end
end
例2:中間テーブル
class CreateEntries < ActiveRecord::Migration
def change
create_table :entries do |t|
t.references :user, index: true, foreign_key: true
t.references :job, index: true, foreign_key: true
t.timestamps
end
end
end
③マイグレーションファイルの実行
$ pwd
現在のディレクトリが正しいことを確認
$ rake db:migrate
マイグレーションファイルの実行
④データベースでデータを確認
④データベースを戻す場合
rake db:rollback
マイグレーションファイルの削除