I have two csv files. One has this header
%w{ Name E-mail Job Phone Application_date }
the other one has
%w{ E-mail Note }
What i want is to merge the two in a unique CSV..with this header
%w { Name E-mail Job Phone Application_date Note }
In the process as you already figured out i want to pair the Note column data with the relative E-mail of the first CSV, because the e-mails of the second CSV are present in the first CSV. So i need to pair the Note column data with throught the e-mail..
require 'csv'
desc "Import csv candidates into the database"
task candidates: :environment do
filepath_candidates_csv = 'data/Import task - Candidates.csv'
filepath_note_csv = 'data/Import task - Notes.csv'
filepath_final_csv = 'data/Final.csv'
#removing candidates duplicates from the csv
candidates = CSV.read(filepath_candidates_csv)
new_candidates = candidates.uniq {|x| x.first}
# removing candidates notes from the csv
notes = CSV.read(filepath_note_csv)
new_notes = notes.uniq {|x| x.first}
new_notes[0][0] = "E-mail"
# generate new csv array with the updated fields
hs = %w{ Name E-mail Phone Job Created_at Note }
CSV.open(filepath_final_csv, "wb") do |csv|
csv << hs
CSV.parse_line(new_candidates) do |line|
csv << line unless line.contain?("E-mail")
end
end
end
i get this error
Running via Spring preloader in process 9372
rake aborted!
NoMethodError: private method `gets' called for #<Array:0x00005638b5452bc8>
/home/luis/code/levisn1/Import-Task/csv_Importer/lib/tasks/import.rake:23:in `block (2 levels) in <main>'
/home/luis/code/levisn1/Import-Task/csv_Importer/lib/tasks/import.rake:21:in `block in <main>'
-e:1:in `<main>'
Tasks: TOP => candidates
(See full trace by running task with --trace)