Skip to content

Instantly share code, notes, and snippets.

@jonahoffline
Last active December 18, 2015 15:58
Show Gist options
  • Save jonahoffline/5807631 to your computer and use it in GitHub Desktop.
Save jonahoffline/5807631 to your computer and use it in GitHub Desktop.
Rake file for importing a CSV file into an Active Record table.
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