I have two CSV files: "users" and "enrollments":
001.csv:
user_id,user_name,state
12345,test_account,active
002.csv:
course_id,user_id,state
67890,12345,active
I need to create one file like active_enrollments.csv:
course_id,user_name
67890,test_account
Without looping through the files multiple times, how can I parse these to generate the file active_enrollments.csv?
Here is what I have so far, but I am getting a lot of duplicates:
require 'csv'
CSV.open("active-enrollments.csv", "wb") do |csv|
csv << ["course_id", "user_name", "user_id","course_name", "status"]
end
Dir["csvs/*.csv"].each do |file|
#puts file
CSV.foreach(file, :headers => true) do |row|
if row['user_id'] && row ['course_id'] #finds enrollment csvs
if row['state'] == "active" #checks for active enrollments
state = row['state']
course_id = row['course_id']
user_id = row['user_id']
Dir["csvs/*.csv"].each do |files|
CSV.foreach(files, :headers => true) do |user|
if user['user_name']
if user_id == user['user_id']
user_name = user['user_name']
Dir["csvs/*.csv"].each do |file|
CSV.foreach(file, :headers => true) do |courses|
if course_id == courses['course_id']
course_name = courses['course_name']
CSV.open("active-enrollments.csv", "a") do |csv|
csv << [course_id, user_name, user_id, course_name, state]
end
end
end
end
end
end
end
end
end
end
end
end
I know this is simple, but I can't seem to get it without looping through the files multiple times and generating lots of duplicates.