0
0
Ruby on Railsframework~20 mins

Model generation in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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?
Arails generate model Article title:text content:string
Brails generate model Article title:string content:text
Crails generate scaffold Article title:string content:text
Drails generate model article title:string content:text
Attempts:
2 left
💡 Hint
Model names should be singular and capitalized. Attribute types must match the desired data type.
component_behavior
intermediate
2: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.save
AThe save ignores validations and saves the product.
BThe save returns true and saves the product with a blank name.
CThe save raises a runtime error due to missing name.
DThe save returns false and the product is not saved to the database.
Attempts:
2 left
💡 Hint
Think about what validations do before saving a record.
🔧 Debug
advanced
2: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?
AThe migration file was deleted or renamed after generation, causing Rails to not find it.
BThe model file <code>user.rb</code> is missing in app/models.
CThe database.yml file is missing the development database configuration.
DThe schema.rb file is corrupted and needs to be deleted.
Attempts:
2 left
💡 Hint
Check the migration files folder for the generated migration.
state_output
advanced
2: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?
AMigration fails with a syntax error because 'class' is a reserved Ruby word.
BRails renames the 'class' attribute automatically to 'class_field'.
CMigration runs successfully and creates a posts table with a class column.
DMigration runs but the 'class' column is ignored.
Attempts:
2 left
💡 Hint
Consider Ruby reserved keywords and their use as column names.
🧠 Conceptual
expert
3: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?
Arails generate model Comment body:text commentable:references{polymorphic}
Brails generate model Comment body:text commentable_id:integer commentable_type:string
Crails generate model Comment body:text commentable:belongs_to
Drails generate model Comment body:text commentable:polymorphic
Attempts:
2 left
💡 Hint
Rails has a special syntax for polymorphic references in generators.