0
0
Ruby on Railsframework~20 mins

Seed data in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Seed Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output after running this seed file?
Given this Rails seed file snippet, what will be the count of users in the database after running rails db:seed?
Ruby on Rails
User.create([{ name: 'Alice' }, { name: 'Bob' }])
User.create(name: 'Charlie')
User.create(name: 'Alice')
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Each create call adds records unless validations prevent it.
📝 Syntax
intermediate
2:00remaining
Which seed syntax will cause a syntax error?
Identify which option will cause a syntax error when running rails db:seed.
AUser.create(name: 'Dave', age: 30)
BUser.create([{ name: 'Eve' }, { name: 'Frank' }])
CUser.create(name: 'Grace' age: 25)
DUser.create([{ name: 'Heidi' }])
Attempts:
2 left
💡 Hint
Check for missing commas between hash keys.
state_output
advanced
2:00remaining
What will be the value of Product.count after seeding?
Consider this seed code snippet. What is the final count of products in the database?
Ruby on Rails
Product.destroy_all
Product.create(name: 'Pen', price: 1.5)
Product.create(name: 'Notebook', price: 3.0)
Product.create(name: 'Pen', price: 1.5)
A3
B2
C0
D1
Attempts:
2 left
💡 Hint
Destroy_all clears all records before creating new ones.
🔧 Debug
advanced
2:00remaining
Why does this seed file fail with a validation error?
This seed file fails when run. What is the most likely cause?
Ruby on Rails
Category.create(name: 'Books')
Category.create(name: 'Books')
AThe database connection is not established.
BThe <code>Category</code> model does not allow creation without an ID.
CThe seed file syntax is invalid due to missing commas.
DThe <code>name</code> field must be unique and duplicates cause validation failure.
Attempts:
2 left
💡 Hint
Check model validations for uniqueness constraints.
🧠 Conceptual
expert
3:00remaining
What is the best way to ensure idempotent seed data in Rails?
You want to run rails db:seed multiple times without creating duplicate records. Which approach achieves this?
AUse <code>Model.find_or_create_by</code> with unique attributes to avoid duplicates.
BUse <code>Model.create</code> without checks; duplicates are acceptable.
CManually delete all records before seeding with <code>Model.destroy_all</code>.
DRun <code>rails db:seed</code> only once and never again.
Attempts:
2 left
💡 Hint
Think about how to check if a record exists before creating it.