Last active
December 16, 2015 23:49
-
-
Save trevorturk/5516046 to your computer and use it in GitHub Desktop.
Insert an additional, separate set of fixtures from db/seeds
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
desc "Insert an additional, separate set of fixtures from db/seeds" | |
task :seed_fixtures => :environment do | |
require 'active_record/fixtures' | |
files = "#{Rails.root}/db/seeds/*.yml" | |
connection = ActiveRecord::Base.connection | |
fixtures = Dir[files].collect do |file| | |
extname = File.extname(file) | |
name = File.basename(file).chomp(extname) | |
class_name = name.classify | |
path = file.chomp(extname) | |
ActiveRecord::FixtureSet.new(connection, name, class_name, path) | |
end | |
connection.transaction do | |
fixtures.each do |file| | |
file.table_rows.each do |name, rows| | |
rows.each do |row| | |
connection.insert_fixture(row, name) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment