Recall & Review
beginner
What is the purpose of generating a model in Rails?
Generating a model in Rails creates a Ruby class that represents a table in the database. It helps manage data and business logic related to that table.Click to reveal answer
beginner
Which command generates a new model named 'Article' with a 'title' string and 'body' text fields?
rails generate model Article title:string body:text
Click to reveal answer
intermediate
What files are created when you run the Rails model generation command?
Rails creates a model file (e.g., app/models/article.rb), a migration file to create the database table, and test files for the model.
Click to reveal answer
beginner
Why do you need to run 'rails db:migrate' after generating a model?
Because the migration file created by the model generator defines the database changes. Running 'rails db:migrate' applies those changes to the database.
Click to reveal answer
intermediate
How can you add validations to a Rails model after generation?
You add validation methods inside the model class, like validates :title, presence: true to ensure data meets rules before saving.
Click to reveal answer
What does the Rails model generation command create besides the model file?
✗ Incorrect
The model generation command creates a migration file to define the database table structure.
Which command applies the database changes after generating a model?
✗ Incorrect
'rails db:migrate' runs the migration files to update the database schema.
How do you specify a string field named 'name' when generating a model?
✗ Incorrect
The syntax is 'field_name:data_type', so 'name:string' defines a string field named 'name'.
Where is the model file located after generation?
✗ Incorrect
Model files are stored in the app/models directory.
What is the main role of a Rails model?
✗ Incorrect
Models handle data and business rules in Rails applications.
Explain the steps and commands to create a new model in Rails and make it ready to use with the database.
Think about how Rails connects code to the database.
You got /5 concepts.
Describe how you can add rules to a Rails model to check data before saving it.
Consider how to keep data clean and correct.
You got /3 concepts.