When the environment construction is completed to some extent and it is reflected in the application development, first create the model and table. Here is a brief summary of the basics of creating a model.
rails g model [model name] column1: data type, column2: data type, ...
rails g model [model name] [references]: * references *
rails g model
command, the following 4 files are automatically generated. Of these, 1 and 2 will appear this time, so keep them in check.1.Model class file(/app/models/[Model name].rb)
2.Migration file(/db/migrate/yyyyMMddxxxx_create_[table name])
3.Automatic model testing
4.Ficture file used for automatic model testing
Execute the rails db: migrate
command
rails g model
commandThere is the following description, so correct it (add)
/db/migrate/yyyyMMddxxxx_create_[table name]
t.references :[Own table name singular]`
↓
/db/migrate/yyyyMMddxxxx_create_[table name]
`t.references :[Own table name singular], foreign_key:true`
Create / update table
rails g migration [class name] [reference class name]: references, ...
execute the command
Create / update table
Recommended Posts