Create Useful Seed Data for Development

Learn to create seed data for the development in our example.

We'll cover the following

Add seed data

Rails’ documentation is unclear on the purpose of seed data, but it’s commonly used to seed development data, and that’s how we view it as well. Because we have set up Factory Bot to create realistic, yet fake data for tests, we can use that for our seed data, too.

There are a few considerations for seed data. First, it should run only in development, so we’ll need to check for that. Second, it should ideally be idempotent without requiring a full database reset. We might not be able to do this entirely in the seed data file when the data model gets more complex, but for now, we can, so we’ll use destroy_all to delete all the data first.

Lastly, we want data that is useful in building our UI and exercising the app manually. To that end, we want to make sure a widget exists so that we can exercise trying to use the same name for two widgets belonging to the same manufacturer.

Because we are using Faker, it could be annoying to have randomly changing names, so for this particular case, we’ll give explicit names. We could give explicit names for everything if we like; it all depends on what we need from the development data.

We’ll replace db/seeds.rb with the following:

Get hands-on with 1200+ tech skills courses.