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?
AA migration file to create the database table
BA controller file
CA view template
DA stylesheet 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?
Arails db:migrate
Brails generate migration
Crails server
Drails console
✗ 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?
Aname_text
Bstring:name
Cfield:name:string
Dname:string
✗ 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?
Adb/migrate/
Bapp/controllers/
Capp/models/
Dapp/views/
✗ Incorrect
Model files are stored in the app/models directory.
What is the main role of a Rails model?
ARoute web requests
BManage data and business logic
CHandle user input forms
DDisplay data to users
✗ 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.
Practice
(1/5)
1. What does the rails generate model command do in a Rails application?
easy
A. It creates a new model file and a migration file for the database.
B. It runs the database migration to update the schema.
C. It starts the Rails server to test the application.
D. It creates a new controller with views.
Solution
Step 1: Understand the purpose of rails generate model
This command creates a new model file that defines the data structure and a migration file to set up the database table.
Step 2: Differentiate from other commands
Running migrations is a separate step, and starting the server or creating controllers are different commands.
Final Answer:
It creates a new model file and a migration file for the database. -> Option A
Quick Check:
Model generation = creates model and migration files [OK]
Hint: Model generation creates files, migration applies changes [OK]
Common Mistakes:
Confusing model generation with running migrations
Thinking it starts the server
Mixing up model and controller generation
2. Which of the following is the correct syntax to generate a model named Article with a string field title and a text field content?
easy
A. rails generate model Article title:text content:string
B. rails generate model Article title:string content:text
C. rails generate controller Article title:string content:text
D. rails generate model article title:string content:text
Solution
Step 1: Check the command structure
The correct command uses 'rails generate model' followed by the model name with capitalized first letter and fields with their types.
Step 2: Verify field types and names
rails generate model Article title:string content:text correctly uses 'title:string' and 'content:text'. rails generate model Article title:text content:string swaps types incorrectly. rails generate controller Article title:string content:text uses 'controller' instead of 'model'. rails generate model article title:string content:text uses lowercase model name which is incorrect convention.
Final Answer:
rails generate model Article title:string content:text -> Option B
Quick Check:
Correct syntax = rails generate model Article title:string content:text [OK]
Hint: Model name capitalized, fields as name:type pairs [OK]
Common Mistakes:
Using 'controller' instead of 'model'
Swapping field types
Using lowercase model name
3. After running rails generate model Product name:string price:decimal and then rails db:migrate, what will be the result when you check the database schema?
medium
A. A products table with columns id, name (string), price (decimal), created_at, and updated_at.
B. A products table with columns id, name (decimal), price (string), created_at, and updated_at.
C. No table created until you manually create it in the database.
D. A products table with only id and timestamps columns.
Solution
Step 1: Understand model generation and migration
The generate command creates a migration file defining a products table with name as string and price as decimal. Running 'rails db:migrate' applies this to the database.
Step 2: Check expected schema
The table will have an id primary key, the specified columns with correct types, and timestamps (created_at, updated_at) added by default.
Final Answer:
A products table with columns id, name (string), price (decimal), created_at, and updated_at. -> Option A
Quick Check:
Migration creates table with correct columns and timestamps [OK]
Hint: Migration creates table with fields and timestamps [OK]
Common Mistakes:
Mixing up field types in the schema
Thinking migration doesn't create tables
Ignoring default timestamps columns
4. You ran rails generate model User email:string but forgot to run rails db:migrate. What problem will you face if you try to create a new User record?
medium
A. Rails will automatically run the migration for you.
B. The User record will save successfully without errors.
C. ActiveRecord::StatementInvalid error because the users table does not exist.
D. The model file will be missing, causing a LoadError.
Solution
Step 1: Understand migration role
Generating the model creates files but does not update the database schema until migrations are run.
Step 2: Predict error when saving record
Without migration, the users table does not exist, so trying to save a User record causes a database error.
Final Answer:
ActiveRecord::StatementInvalid error because the users table does not exist. -> Option C
Quick Check:
Missing migration run = database table missing error [OK]
Hint: Always run migrations after generating models [OK]
Common Mistakes:
Assuming migration runs automatically
Expecting record to save without table
Confusing missing model file with migration issue
5. You want to generate a model Order with a status field that can only be 'pending', 'paid', or 'shipped'. Which approach correctly sets this up in Rails?
hard
A. Generate model with status:enum field directly in the generate command.
B. Generate model with status:boolean field to represent the three states.
C. Generate model without status field and add it later manually in the database.
D. Generate model with status:string, then add an enum in the model file for the allowed values.
Solution
Step 1: Understand Rails enum usage
Rails does not support enum type in migrations directly. Instead, you create a string or integer field and define enum in the model file.
Step 2: Choose correct field type and setup
Using 'status:string' in the generate command and then adding enum in the model file for 'pending', 'paid', 'shipped' is the correct approach.
Final Answer:
Generate model with status:string, then add an enum in the model file for the allowed values. -> Option D
Quick Check:
Enum = string field + model enum definition [OK]
Hint: Use string field plus enum in model, not enum type in migration [OK]
Common Mistakes:
Trying to use enum type directly in migration
Using boolean for multiple states
Skipping field creation in generate command
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.