The seed-dump gem generates db/seeds.rb
for a Rails project from an existing database. It also works outside of Rails with
standalone-migrations, but you
need to jump through a couple of hoops to set up the Rake task and give it access
to your model classes.
In your Gemfile:
gem 'seed_dump'
In your Rakefile:
$LOAD_PATH.unshift(File.expand_path('app/models', __dir__))
Dir.glob(File.expand_path('app/models/*.rb', __dir__)).sort.each(&method(:require))
require 'seed_dump'
namespace :db do
namespace :seed do
desc "Dump records from the database into db/seeds.rb"
task :dump => :environment do
SeedDump.dump_using_environment(ENV)
end
end
end