Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Created February 12, 2020 17:42
Show Gist options
  • Save dmolesUC/afa63a7a0d2a5d1634f3c4c75b88e1a9 to your computer and use it in GitHub Desktop.
Save dmolesUC/afa63a7a0d2a5d1634f3c4c75b88e1a9 to your computer and use it in GitHub Desktop.
Using the seed_dump gem with StandaloneMigrations

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment