lundi 14 septembre 2015

How do I group different rows into one row by date

I have these ruby method.

@averages = Array.new
  weekly_averages.each do |average|

  yr = average.date.to_s[0..3].to_i
  wk = average.date.to_s[4..5].to_i
  prices = Array.new
  prices << { locale: average.locale, price: average.average }

  @averages << { :year => yr, :week => wk, :prices => prices }

end

This results into a table like this

Week Ending Date Product    Nairobi  Mombasa   Kisumu    Nakuru  Eldoret
38  2014-09-21  Dry Maize   3,140.00                
38  2014-09-21  Dry Maize           2,216.67            
38  2014-09-21  Dry Maize                     2,513.13      
38  2014-09-21  Dry Maize                               3,362.50    
38  2014-09-21  Dry Maize                                        2,311.43`

How can I collapse this into just one row?

something like..

Week Ending Date   Product  Nairobi Mombasa Kisumu  Nakuru   Eldoret
38      2014-09-21      Dry Maize 3,140   2216.67 2,513.13 3,362.50 2,311.43

Aucun commentaire:

Enregistrer un commentaire