Ruby on Railsでテーブルを作成する方法を記述します。
1:マイグレーションファイル作成
テーブルの設計図であるマイグレーションファイルを生成します。
テーブル名
User
カラム一覧
カラム名 | データ形 |
---|---|
id | integer |
name | string |
password | string |
created_at | datetime |
updated_at | datetime |
console
rails g model User name:string password:string
*id、crated_at、updated_atは自動的に作られるので記述しない
作成されたマイグレーションファイルは、
db ⇨ migrateフォルダ内に、「(年月日時分秒)createモデル名(複数形).rb」として生成されています。
2:マイグレーションファイルを反映させる
作成したマイグレーションファイルをデータベースに適用させます。
console
rails db:migrate
以上でテーブルを生成することができます。