Skip to content

Instantly share code, notes, and snippets.

@col
Created August 30, 2012 03:43
Show Gist options
  • Save col/3522085 to your computer and use it in GitHub Desktop.
Save col/3522085 to your computer and use it in GitHub Desktop.
Simple rake task to import data from CSV
#
# Taken from "Ten Things You Didn't Know Rails Could Do by James Edward Gray"
# http://www.youtube.com/watch?v=GRfJ9lni4QA
#
require 'csv'
namespace :users do
desc "Import users from a CSV file"
task :import => :environment do
path = END.fetch("CSV_FILE") {
File.join(File.dirname(__FILE__), %w[.. .. db data users.csv])
}
CSV.foreach(path, headers: true, header_converters: :symbol) do |row|
User.create(row.to_hash)
end
end
end
@kewpiedoll
Copy link

path = ENV.fetch ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment