I have a members table from which I pull:member_id, :client_uid, :first_name, and:last_name. The Members table has a has_many association to Quality Measures from which I pull:measure and:measure_status for each association and flatten it.
I am trying to get the desired Members data and the flattened Quality Measure data into one CSV row.
Since there can be any number of Quality Measure associated records, I get the max number of associated records and create that number of Measure and Measure Status columns and create the proper number of headers:
max_qm_associations = get_max_qm_associations(filtered_members_ids)
column_headers = ["Member ID", "Client UID","First Name", "Last Name"]
max_qm_associations.times do |qm|
column_headers += ["Measure", "Measure Status"]
end
so that the desired output appears like the following assuming the max number of Quality Measure associated records for my Members is 2. Therefore:
Member ID | Client UID | First Name | Last Name | Measure | Measure Status | Measure | Measure Status |
---|---|---|---|---|---|---|---|
1 | 23232 | John | Doe | Drink Coffee | Met | Eat | Not Met |
2 | 32323 | Jane | Doe | Walk | Met | none | none |
So far I've tried many things. My most recent looks nice enough (but not the goal) by putting the Measure - Measure Status in the first Measure column:
csv = CSV.generate(headers: true) do |csv|
# Add headers
csv << column_headers
filtered_members.each do |member|
# Add member data
qm_measures = ""
member.quality_measures.each do |qm|
qm_measures << [qm.measure, qm.measure_status.upcase].join(' - ') + "\n"
end
csv << [member.id, member.client_uid, member.first_name.humanize, member.last_name.humanize, qm_measures]
end
end
The result:
Member ID | Client UID | First Name | Last Name | Measure | Measure Status | Measure | Measure Status |
---|---|---|---|---|---|---|---|
1 | 23232 | John | Doe | Drink Coffee - Met \n Eat - Not Met | |||
2 | 32323 | Jane | Doe | Walk - Met \n |
Another attempt simply did the above but only as comma separated array values:
csv = CSV.generate(headers: true) do |csv|
# Add headers
csv << column_headers
filtered_members.each do |member|
# Add member data
csv << [member.id, member.client_uid, member.first_name.humanize, member.last_name.humanize, member.quality_measures.map {|qm| "#{qm.measure}, #{qm.measure_status}"}.flatten]
end
end
This result:
Member ID | Client UID | First Name | Last Name | Measure | Measure Status | Measure | Measure Status |
---|---|---|---|---|---|---|---|
1 | 23232 | John | Doe | "Drink Coffee,Met", "Eat,Not Met" | |||
2 | 32323 | Jane | Doe | "Walk,Met" |
Again, as a refresher, my desired output is as follows:
Member ID | Client UID | First Name | Last Name | Measure | Measure Status | Measure | Measure Status |
---|---|---|---|---|---|---|---|
1 | 23232 | John | Doe | Drink Coffee | Met | Eat | Not Met |
2 | 32323 | Jane | Doe | Walk | Met | none | none |
Edits: Table formats keep breaking on submit.
The problem seems to come from this part member.quality_measures.map {|qm| "#{qm.measure}, #{qm.measure_status}"}.flatten
member.quality_measures.map {|qm| "#{qm.measure}, #{qm.measure_status}"}.flatten
it returns you the following array ["Drink Coffee,Met", "Eat,Not Met"]
You would probably want to do this in your code:
member.quality_measures.map{|qm| "#{qm.measure}", "#{qm.measure_status}"}.flatten
that will return the ["Drink Coffee","Met", "Eat","Not Met"] and then you would possibly want to flatten the whole array that you append like so:
csv << [member.id, member.client_uid, member.first_name.humanize, member.last_name.humanize, member.quality_measures.map {|qm| "#{qm.measure}", "#{qm.measure_status}"}.flatten].flatten
I did not test it yet but it should work if you go in that direction.
Thanks to @iseitz!
I was only one.flatten away from my desired output.
csv = CSV.generate(headers: true) do |csv|
# Add headers
csv << column_headers
filtered_members.each do |member|
qms = member.quality_measures.map {|qm| ["#{qm.measure}", "#{qm.measure_status}"]}.flatten
csv << [member.id, member.client_uid, member.first_name.humanize, member.last_name.humanize, qms].flatten
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.