Challenge - 5 Problems
Seed Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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')Attempts:
2 left
💡 Hint
Each
create call adds records unless validations prevent it.✗ Incorrect
The first line creates 2 users, second line adds 1 more, and the third adds another user named 'Alice'. Total is 4 users.
📝 Syntax
intermediate2:00remaining
Which seed syntax will cause a syntax error?
Identify which option will cause a syntax error when running
rails db:seed.Attempts:
2 left
💡 Hint
Check for missing commas between hash keys.
✗ Incorrect
Option C is missing a comma between
name: 'Grace' and age: 25, causing a syntax error.❓ state_output
advanced2: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)
Attempts:
2 left
💡 Hint
Destroy_all clears all records before creating new ones.
✗ Incorrect
All three
create calls add products. No uniqueness validation is shown, so duplicates are allowed. Total is 3.🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
Check model validations for uniqueness constraints.
✗ Incorrect
If
name is validated as unique in the model, creating two categories with the same name causes a validation error.🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about how to check if a record exists before creating it.
✗ Incorrect
Using
find_or_create_by checks for existing records with given attributes and creates only if none exist, making seeds safe to run multiple times.