Skip to content

Instantly share code, notes, and snippets.

@danielpaul
Created May 10, 2016 17:01
Show Gist options
  • Save danielpaul/46dc3843738735a426dc0f2b8d84e4dd to your computer and use it in GitHub Desktop.
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
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