Last active
December 18, 2015 15:58
-
-
Save jonahoffline/5807631 to your computer and use it in GitHub Desktop.
Rake file for importing a CSV file into an Active Record table.
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
require "csv" | |
desc "Import CSV file into an Active Record table" | |
task :csv_model_import, [:filename, :model] => :environment do |task,args| | |
firstline=0 | |
keys = {} | |
CSV.foreach(args[:filename]) do |row| | |
if (firstline==0) | |
keys = row | |
firstline=1 | |
next | |
end | |
params = {} | |
keys.each_with_index do |key,i| | |
params[key] = row[i] | |
end | |
Module.const_get(args[:model]).create(params) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment