Image for post
Image for post
Photo by Andriyko Podilnyk on Unsplash

How To “Seed” With Ruby on Rails

Kubilay Çağlayan
Nov 9 · 3 min read

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.

We have three options to activate this file:

  1. When the database already created rails db:seed would be the best option.
  2. 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.
  3. 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.

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

Thank you for reading 😊

kubilaycaglayan.com 👋

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Kubilay Çağlayan

Written by

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Kubilay Çağlayan

Written by

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store