Created
June 24, 2012 21:22
-
-
Save vltsu/2985053 to your computer and use it in GitHub Desktop.
import from a CSV file
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' | |
namespace :users do | |
desc "Import users from csv" | |
task :import => :environment do | |
path = ENV.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment