Created
August 30, 2012 03:43
-
-
Save col/3522085 to your computer and use it in GitHub Desktop.
Simple rake task to import data from CSV
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
# | |
# 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
commented
Apr 22, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment