Member-only story

Rails Strategy For Better Structured Seeds For Specific Environments

Dale Zak
2 min readApr 9, 2020

--

This strategy can help better structure your seeds.rb file in Rails to ensure models are populated in order as well as provide environment specific seeds. Enjoy!

The seeds.rb file is the place where you can populate your database with some sample records. So after doing a rake db:reset, the database will contain all the records you created in the seeds.rb file. However there are two problems with the default setup.

One, if you have lots of models, this file can grow pretty large which is difficult to maintain.

Two, if you want certain records to only get created for specific environments, for example some on development but not production, you’d need lots of if-else logic to handle this.

To overcome these problems, I like to follow this strategy for structuring my seeds.rb file.

Create Seeds Folder

First create db/seeds folder, this is where seed files for all environments will live.

mkdir db/seeds

Create Production Seeds Folder

Second create db/seeds/production folder, where only production seed files will go.

 mkdir db/seeds/production

--

--

Dale Zak
Dale Zak

Written by Dale Zak

Full stack developer specializing in web apps built on Rails with Stimulus, and mobile apps using Ionic and Vue.

No responses yet