0
0
Ruby on Railsframework~10 mins

Model generation in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Model generation
Run model generator command
Create model file with class
Create migration file
Edit migration if needed
Run migration to update DB
Model ready to use in app
The flow shows how running the Rails model generator creates files and updates the database schema step-by-step.
Execution Sample
Ruby on Rails
rails generate model Article title:string body:text
rails db:migrate
This command creates an Article model with title and body fields, then updates the database.
Execution Table
StepActionFile Created/ModifiedContent SummaryResult
1Run 'rails generate model Article title:string body:text'app/models/article.rbDefines Article class inheriting from ApplicationRecordModel file created
2Run 'rails generate model Article title:string body:text'db/migrate/XXXXXXXXXXXXXX_create_articles.rbMigration file to create articles table with title:string and body:textMigration file created
3Edit migration file (optional)db/migrate/XXXXXXXXXXXXXX_create_articles.rbAdd/remove/change columns if neededMigration file ready
4Run 'rails db:migrate'Database schemaCreates articles table with specified columnsDatabase updated
5Use Article model in appapp/models/article.rbModel connected to articles tableModel ready for use
💡 All files created and database updated, model generation complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
Model fileNoneCreated with Article classUnchangedUnchangedReady
Migration fileNoneCreated with create_articlesEdited if neededExecuted to update DBApplied
Database schemaNo articles tableNo changeNo changeArticles table createdUpdated
Key Moments - 2 Insights
Why do we need to run 'rails db:migrate' after generating the model?
Generating the model creates a migration file but does not change the database until you run 'rails db:migrate' which applies the migration and creates the table.
What happens if I forget to specify fields when generating the model?
The model file will be created but the migration will create an empty table with only an id and timestamps, so you need to add fields manually or regenerate.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what file is created at Step 1?
Aconfig/routes.rb
Bdb/migrate/XXXXXXXX_create_articles.rb
Capp/models/article.rb
Dapp/controllers/articles_controller.rb
💡 Hint
Check the 'File Created/Modified' column at Step 1 in the execution table
At which step is the database schema updated?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Result' column for when the database is updated
If you skip editing the migration file, what happens?
ADatabase table will be created with default fields only
BMigration will fail to run
CModel file will not be created
DRails will auto-generate fields based on model code
💡 Hint
Refer to the key moment about missing fields and migration file editing
Concept Snapshot
rails generate model ModelName field:type ...
Creates model file and migration file
Edit migration to customize fields
Run rails db:migrate to update database
Model connects to table named plural of ModelName
Use model in app after migration
Full Transcript
Model generation in Rails starts by running the command 'rails generate model' with the model name and fields. This creates a model file defining the class and a migration file to create the database table. You can edit the migration file to adjust fields. Then you run 'rails db:migrate' to apply the migration and update the database schema. After this, the model is ready to use in your application code.