Challenge - 5 Problems
Rails Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct Rails model generation command
Which command correctly generates a Rails model named
Article with a string attribute title and a text attribute content?Attempts:
2 left
💡 Hint
Model names should be singular and capitalized. Attribute types must match the desired data type.
✗ Incorrect
Option B uses the correct command to generate a model named Article with the specified attributes and types. Option B swaps the types incorrectly. Option B generates a scaffold, not just a model. Option B uses a lowercase model name, which does not follow conventions.
❓ component_behavior
intermediate2:00remaining
Behavior of generated model with validations
Given a Rails model
Product generated with name:string and a validation validates :name, presence: true, what happens when you try to save a Product without a name?Ruby on Rails
class Product < ApplicationRecord
validates :name, presence: true
end
p = Product.new
p.saveAttempts:
2 left
💡 Hint
Think about what validations do before saving a record.
✗ Incorrect
The validation prevents saving a product without a name. The save method returns false and the record is not saved. No error is raised.
🔧 Debug
advanced2:00remaining
Fixing migration timestamp mismatch error
You generated a model with
rails generate model User email:string but when running rails db:migrate, you get an error about a missing migration file. What is the most likely cause?Attempts:
2 left
💡 Hint
Check the migration files folder for the generated migration.
✗ Incorrect
Rails relies on migration files with timestamps to run migrations. If the migration file is missing or renamed, Rails cannot find it and raises an error. The model file missing does not affect migration. Database config or schema.rb issues cause different errors.
❓ state_output
advanced2:00remaining
Result of running a model generator with a reserved word attribute
What happens if you run
rails generate model Post class:string and then try to migrate?Attempts:
2 left
💡 Hint
Consider Ruby reserved keywords and their use as column names.
✗ Incorrect
Option C is correct. The generated migration uses `t.string :class`, which is valid Ruby syntax (:class is a symbol) and runs successfully, creating the table with a class column. Although 'class' is reserved, it does not cause a syntax error. Rails does not rename or ignore it.
🧠 Conceptual
expert3:00remaining
Understanding Rails model generation with polymorphic associations
You want to generate a model
Comment that belongs to a polymorphic association called commentable. Which command correctly generates the model with the necessary fields?Attempts:
2 left
💡 Hint
Rails has a special syntax for polymorphic references in generators.
✗ Incorrect
Option A uses the correct syntax with references{polymorphic} to generate both commentable_id and commentable_type columns. Option A manually specifies columns but is not the generator syntax. Options C and A use invalid syntax.