Stop using the seed file to populate your dev data in Rails

Henry Tabima Giraldo
3 min readFeb 18, 2020

If you are populating your development data using the seed file, you are doing it wrong. In this post, I’m going to show you the best way to do it. You will like it, I promise.

Let’s see what thoughtbot guys have to say about it :

Note that there’s a subtle difference between development data and seed data; db/seeds.rb should be reserved for static data that the application requires, like the fifty US states, and be intended to run in production. (https://thoughtbot.com/upcase/videos/factory-bot#using-factorybot-to-generate-development-data)

“So… what should I do?”

The process is quite simple, you are going to create a custom rake command that has all the logic to populate your data. That could be done just with the Models classes, but in this tutorial, I’m going to show you how to do it better with Factory Bot and Faker.

Creating Custom rake command

You can find the respective documentation [here] or [here].

The first thing we need to do is to create a .rake file inside the libs/tasks folder. If the folders don’t exist, create it. The name of the file doesn’t matter. For this example, I’m going to call it, and it goes like this:

The first line is to make sure that this command only runs in development or test environments. The namespace:dev and task populate defines the name of your command. For this specific example, if you want to run it, you have to write rails dev:populate in your terminal. Last but not least, after the task populate: you can see 'db:migrate:reset', this means that it is going to run rails db:migrate:reset before it starts running my code. I choose db:migrate:reset because I want to clean my development database before populating it again.

If you want to use your Models classes to populate your date, this should be enough. You can do something like:

but let’s use factory bot and faker. Make sure you have installed the gems before continuing.

Using Factory Bot and Faker

You can find the official documentation for Factory Bot [here] and Faker’s documentation [here].

First, I’m going to create a file factories.rb in the project’s root path, and the code inside goes like this:

To better understand the previous code, please read the official docs.

Now, with our factories defined, we have to modify our custom rake file.

We have to require Factory Bot in our file. Then, you can add the include line to be able to use create, create_list and many other methods directly (you can omit this and use the methods like: FactoryBot.create(...)).

That’s all. Now you can run rails dev:populate and this is going to recreate your database with 3 users and 3 posts for each user. Now you can write your own script that matches your project’s requirements.

I hope this post helps you along your path. If you did, I would love to hear from you! I’m on Twitter as @HenryTabimaG and GitHub as @HenryTabima. If you want any specific content for a post, feel free to ask.

--

--