Created
May 10, 2016 17:01
-
-
Save danielpaul/46dc3843738735a426dc0f2b8d84e4dd to your computer and use it in GitHub Desktop.
Read a CSV and match details from DB and save new columns back to new 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
namespace :csv do | |
desc "TODO" | |
# to run: rake csv:match | |
task match: :environment do | |
puts "Starting..." | |
csv_text = File.read('./tmp/exported-report.csv') | |
csv = CSV.parse(csv_text, :headers => true, :return_headers => true) | |
save_file = "./tmp/sorted-data.csv" | |
CSV.open( save_file, 'w' ) do |writer| | |
csv.each do |row| | |
user = User.find_by_email(row['Please confirm your email address'].downcase) | |
if user | |
row['user_name'] = user.full_name | |
row['user_id'] = user.id | |
puts "Updated #{user.full_name}'s record." | |
end | |
writer << row | |
end | |
end | |
puts "... Finished!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment