Created
March 11, 2015 17:27
-
-
Save awoodworth/9b5072c3106f25a1f8d2 to your computer and use it in GitHub Desktop.
CSV to Seed-Fu
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
# NOTE: you will want to change the three attributes at the top | |
require 'csv' | |
require 'seed-fu' | |
# change these | |
@csv_file = 'export.csv' | |
@output_file = 'class.rb' | |
@class_name = 'Class' | |
@seeds = CSV.read("#{@csv_file}", { headers: true } ) | |
SeedFu::Writer.write("#{@output_file}", :class_name => "#{@class_name}", :constraints => [:id]) do |writer| | |
@seeds.each do |seed| | |
hash = {} | |
@seeds.headers.reject{|h| h.nil? || h.eql?("created_at") || h.eql?("updated_at")}.each do |header| | |
hash[header.to_sym] = seed[header] | |
end | |
writer.add(hash) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment