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.
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
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:seedwould 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:setupto first run migrations and then seeding.
- And the last option is when we want to reset the database,
rails db:resetwill drop the database, create again, and seed the application.
All of the options above will seed the database with those two records.
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.csvname, age
In this case, I also prefer to use an isolated file to keep the “CSV to model mapping”, like
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
When you are ready with the
db/user_seeding.rb, you can call it in the
You can also use seed commands on Heroku by starting with
heroku run .
heroku run rails db:seed
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.
- https://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data ➡️
- https://guides.rubyonrails.org/active_record_migrations.html#setup-the-database ➡️
- https://guides.rubyonrails.org/active_record_migrations.html#resetting-the-database ➡️
- https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html ➡️
- https://ruby-doc.org/core-2.5.0/File.html#method-c-new ➡️
Thank you for reading 😊