How To “Seed” With Ruby on Rails
This article aims to help people who want to learn about the seed feature in Rails. After this article readers will be able to quickly populate their database with the predefined records.
In a standard Rails project, we have the seed file atdb/seed.rb
.
This file is triggered by the rails db:seed
command and runs in the Rails context. Which means all the model structure will be available within the file.
It is also triggered by the rails db:reset
and rails db:setup
commands.
As it is obvious from the name, the main purpose of this file is to seed the database.
This is a very useful feature especially when you are in the learning stage where you are resetting the database many times in a day.
In such situations, you don’t want to add records to your database manually but instead, you would have them ready in the db/seed.rb
file.
In Action
Let’s imagine we have a Rails application with a User model in which we can create users with name attributions.
If we want our database to be ready with two users after initialization, we can simply create those records in the seed file.
# db/seed.rbputs "Seeding..."User.create(name: 'Matz')
User.create(name: 'DHH')puts "Seeding done."
We have three options to activate this file:
- When the database already created
rails db:seed
would be the best option. - When the database is not ready yet, in other words, we want to create the database first, then we can go with
rails db:setup
to first run migrations and then seeding. - And the last option is when we want to reset the database,
rails db:reset
will drop the database, create again, and seed the application.
All of the options above will seed the database with those two records.
Seeding CSV
On some occasions, we may want to seed the applications with thousands of rows of records.
Imagine we want to transfer data as comma-separated-values, from a different database to the Rails application.
Then we may have a directory to keep the .csv files. Let’s say it is in the db/data/users.csv
.
# db/data/users.csvname, age
Matz, 55
DHH, 41
In this case, I also prefer to use an isolated file to keep the “CSV to model mapping”, like db/user_seeding.rb
.
In the db/user_seeding.rb
we can create the mapping from CSV to our model and create records one by one with the help of CSV class in Ruby. This logic will be encapsulated in a method and be called in the db/seed.rb
file.
db/user_seeding.rb
⏬
When you are ready with the db/user_seeding.rb
, you can call it in the db/seed.rb
⏬
Seeding Heroku
You can also use seed commands on Heroku by starting withheroku run
.
Example: heroku run rails db:seed
Conclusion
Knowing how to seed will help you a lot especially at the development stage.
Ready-to-populate data should always be in your seed files. This way you can focus more on improving the main parts of the application rather than spending your time structuring the initial database state.
Acknowledgments
- https://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data ➡️
rails db:seed
- https://guides.rubyonrails.org/active_record_migrations.html#setup-the-database ➡️
rails db:setup
- https://guides.rubyonrails.org/active_record_migrations.html#resetting-the-database ➡️
rails db:reset
- https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html ➡️
CSV class
- https://ruby-doc.org/core-2.5.0/File.html#method-c-new ➡️
File.new
Thank you for reading 😊