Skip to content

Instantly share code, notes, and snippets.

@dbernheisel
Last active March 29, 2017 15:38
Show Gist options
  • Select an option

  • Save dbernheisel/c4e6176eaad9801895ca0a6d3e658d3a to your computer and use it in GitHub Desktop.

Select an option

Save dbernheisel/c4e6176eaad9801895ca0a6d3e658d3a to your computer and use it in GitHub Desktop.
Seeding for Rails environments
module Seed
def self.sprout
require_seed_modules!
puts "\n======= SEEDING FOR ALL ENVIRONMENTS"
seed_from(Seed::Base)
if Rails.env.development?
puts "\n======= SEEDING FOR DEVELOPMENT"
seed_from(Seed::Development)
end
# continue for each environment as necessary
end
def self.require_seed_modules!
Dir["#{Rails.root}/db/seed/*.rb"].each { |s| require s }
end
def self.seed_from(seed_class)
seeds = seed_class.methods.map(&:to_s).select { |method| method.starts_with? 'seed_' }
seeds.each do |seed|
title = seed[4..-1].titleize
print "Seeding #{title}..."
seed_class.send seed
puts "\rSeeded: #{title} "
end
end
end
Seed.sprout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment