Skip to content

Instantly share code, notes, and snippets.

@tigawa
Last active August 29, 2015 14:04
Show Gist options
  • Save tigawa/a99466564f1615419119 to your computer and use it in GitHub Desktop.
Save tigawa/a99466564f1615419119 to your computer and use it in GitHub Desktop.
rails モデルの作成
1. スケルトンを生成
bin/rails g model Administrator #モデルはクラス名を指定する(単数形)
2. マイグレーションスクリプトファイルの編集
# カラムの追加
t.string :email, null: false
t.boolean :suspended, null: false, dafault: false
t.date :start_date, null: false
# 索引の追加
add_index :administrators, :email_for_index, unique: true #ユニーク検索
add_index :administrators, [ :family_name_kana, :given_name_kana ] #複合索引
3. マイグレーション
bin/rake db:migrate
#テーブルに変更を加える場合
http://www.rubylife.jp/rails/model/index7.html
# 変更する時のコマンド
rails generate migration AddXXX(何でもOK)ToYYYYY(テーブル名) price:integer author:string
ails generate migration RemoveAuthor(項目名)FromTitles(テーブル名) author:string
rake db:migrate:reset   #dbをドロップして、マイグレーションスクリプトファイルに従い作成
rake db:reset   #dbをドロップして、scema.rbに従い作成
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment