Skip to content

Instantly share code, notes, and snippets.

@awoodworth
Created March 11, 2015 17:27
Show Gist options
  • Save awoodworth/9b5072c3106f25a1f8d2 to your computer and use it in GitHub Desktop.
Save awoodworth/9b5072c3106f25a1f8d2 to your computer and use it in GitHub Desktop.
CSV to Seed-Fu
# 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