23 September 2014

Rails provides the command

rake db:seed

to seed the database. However, sometimes we want to have different db seed for different RAIL_ENV or maybe you just need to seed some data repeatedly. Then the task become inappropriate.

Seedbank https://github.com/james2m/seedbank solves this problem.

# in your gemfile
gem 'seedbank'

The directory structure

db/
  seeds.rb
  seeds/
    bar.seeds.rb
    development/
      users.seeds.rb

Now when you issue rake db:seed it will invoke

db/seeds.rb
db/seeds/*

When you issue RAILS_ENV=development rake db:seed

db/seeds.rb
db/seeds/bar.seeds.rb
db/seeds/development/*

And on RAILS_ENV=production rake db:seed

db/seeds.rb
db/seeds/bar.seeds.rb

Or you just want to seed the users, you can do

rake db:seed:development:users