Last active
August 29, 2015 14:04
-
-
Save firedev/5d9cd290da97d3c131e3 to your computer and use it in GitHub Desktop.
Seed.rb - imports records from .yml files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/seeds.rb | |
# | |
# https://gist.github.com/firedev/5d9cd290da97d3c131e3 | |
# | |
# Imports records from .yml files in db/seed/model.yml | |
def put_cycle | |
@r ||= %w(- \\ | /) | |
print((@r << @r.shift)[0]) | |
print "\b" | |
end | |
def seed(class_name) | |
print "#{class_name}: " | |
seed_file = File.join(Rails.root, 'db/seed', "#{class_name.to_s.underscore}.yml") | |
yml = YAML.load_file(seed_file) | |
yml.each do |item| | |
item = class_name.create item[1] | |
if item.valid? | |
put_cycle | |
else | |
puts | |
puts item.errors.inspect | |
puts | |
end | |
end | |
print class_name.count | |
puts | |
end | |
seed Model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment