0
0
Ruby on Railsframework~10 mins

Seed data in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Seed data
Write seed data in db/seeds.rb
Run rails db:seed command
Rails loads seeds.rb file
Execute seed commands
Data inserted into database
Seed data available for app use
Seed data is written in a file and run by a command to fill the database with initial data.
Execution Sample
Ruby on Rails
User.create(name: "Alice", email: "alice@example.com")
Product.create(name: "Book", price: 9.99)
Order.create(user_id: 1, product_id: 1)
This code adds initial users, products, and orders to the database when run.
Execution Table
StepActionCode ExecutedDatabase ChangeOutput/Result
1Create UserUser.create(name: "Alice", email: "alice@example.com")User table: 1 new recordUser Alice added
2Create ProductProduct.create(name: "Book", price: 9.99)Product table: 1 new recordProduct Book added
3Create OrderOrder.create(user_id: 1, product_id: 1)Order table: 1 new recordOrder linked to User 1 and Product 1
4EndNo more commandsNo changeSeeding complete
💡 All seed commands executed, database populated with initial data
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
User count01111
Product count00111
Order count00011
Key Moments - 2 Insights
Why does the order creation use user_id and product_id instead of user and product objects?
Seed files often use IDs directly because the related records must exist first; see execution_table step 3 where user_id: 1 and product_id: 1 link to existing records.
What happens if you run rails db:seed multiple times?
Running seeds multiple times can create duplicate records unless you add checks; the execution_table shows each create adds a new record every time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the database change after step 2?
AOne new user record added
BOne new order record added
COne new product record added
DNo change
💡 Hint
Check the 'Database Change' column in row for step 2
At which step does the order get linked to the user and product?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Code Executed' and 'Output/Result' columns for step 3
If you add a new product in seeds.rb before creating orders, how would the variable tracker change?
AUser count increases after order creation
BProduct count increases earlier, before order creation
COrder count increases before product count
DNo change in counts
💡 Hint
Refer to the 'Product count' row in variable_tracker and think about order of creation
Concept Snapshot
Seed data in Rails:
- Write initial data in db/seeds.rb
- Use create methods to add records
- Run with rails db:seed
- Data populates database tables
- Useful for setup and testing
Full Transcript
Seed data in Rails means writing code in the seeds.rb file to add initial records to the database. When you run the command rails db:seed, Rails loads and runs this file. Each create command adds a new record to the database tables. For example, creating a user, then a product, then an order linking them. The execution table shows each step adding data. Variables like user count increase as records are added. Beginners often wonder why IDs are used to link records; this is because related records must exist first. Running seeds multiple times can add duplicates unless handled. This visual trace helps understand how seed data fills the database step-by-step.