Seed Dump Ruby Gem

Melissa Gonzalez
Adventures in Code
Published in
3 min readAug 24, 2017
Performing a seed dump onto soil will take the genetic information from existing plants and put it in an environment that will allow nature to create new plants with that genetic information. Performing a seed dump in a Rails app will take information from an existing database and save it in a manner that will allow developers to re-create the app with the same database information.

Creating a Back-Up of Your Development Database

What do you do when you’ve created a new Rails app and realize you want to reset your database, but you don’t want to lose your existing test information?

Perhaps, for example, you want to add a related database table that belongs_to an existing table, and you need to move some of the columns from your original table into your new table. How do you go about grabbing all the data from your original table so that you can manipulate it to form the new table?

Alternately, what if you want others to be able to use your database as a starting point when first downloading your app off Github. How would you be able to give them the starting database with only code to pre-populate their own Postgres database when they load your app?

Seed Dump to The Rescue

The Ruby Seed Dump gem is a way to retroactively create a seeds file from an existing database. You may want to do this if you’d previously populated your database manually either in Rails Console or a program such as Postico and need to reset your database for whatever reason.

In class, we used the Seed Dump Gem to grab our initial database information from our sample online store apps. We were just learning about related tables and our assignment was to pull out the images from our Products table to create a separate “Images” table, thus allowing each product to have more than one image.

However, we had created our initial databases using our Rails Console, so it would have been a huge pain to try to manipulate all this information in our console or even in Postico. Enter the Seed Dump Gem to the rescue!!

What is Seed Dump, And How Do I Use It?

The seed dump gem works by taking your existing database and writing the code that would produce that data, and inserting it into the Rails seed file. This is one way to back up your initial database while developing your app. Note that this is generally only used for initial test data, not actual data from users.

To perform a seed dump, add the seed dump gem to your gem file using the code:

gem ‘seed_dump’

To save all tables in your current database to your seeds file, run the following command in your terminal:

rake db:seed:dump

This will populate your db/seed.rb file with all the code necessary to re-create your data.

When to Use A Seed Dump

Reasons for performing a seed dump might include

  • allowing others to test your app using the same initial database you used
  • creating a back-up of existing data that you can manipulate in the event you need to make major changes to the database (which you’d then need to also update the seed file accordingly, then perform a db:reset)
  • having the seed data available as you are debugging your app

For our in-class exercise, we used the Seed Dump to fill in our Seeds file (which at that point, we hadn’t yet learned how to use!). We were then able to pre-populate our new Images table using the data that had already existed in the original “Image” column in our Products table, in addition to adding more images for each product.

Visit the seed dump Github page for more information, including additional features of this gem: https://github.com/rroblak/seed_dump.

--

--

Melissa Gonzalez
Adventures in Code

Aspiring Web Developer. Fitness Enthusiast. Foodie. Beer Lover. Triathlete. Former Research Scientist.