views:

88

answers:

2

I have this data and i need an output like this type of output.. I basically need to have all the venues and their dates and all the songs associated to them ....if anyone can think of a better structure and how to achieve it I would be very thankful...

{ 
["Vector Arena - Auckland Central, New Zealand" => 
    { 
    "2010-10-10" => ["Enter Sandman", "Unforgiven", "And justice for all"]
    }], 
    ["Brisbane Entertainment Centre - Brisbane Qld, Austr..." => 
        { 
        "2010-10-11" => ["Enter Sandman"]
    }]
 }

so far i tried this...and not sure if i am moving in the right direction..

@venues = Hash.new {|hash, key| hash[key] = 0}
@requests = Request.find_all_by_artist("Metallica")
@requests.each do |request|
  @venues[request.venue] =  request.showdate
end
+1  A: 

I think you structure is not quite right. It should be like

[ 
{"Vector Arena - Auckland Central, New Zealand" => 
    { 
    "2010-10-10" => ["Enter Sandman", "Unforgiven", "And justice for all"]
    }}, 
    }"Brisbane Entertainment Centre - Brisbane Qld, Austr..." => 
        { 
        "2010-10-11" => ["Enter Sandman"]
    }}
 ]

Try this code

@venues = []
      all_venues = Request.find(:all, :select => "distinct venue, showdate")
      all_venues.each do |unique_venue|
        venue_hash = {}
        showdate_hash = {}
        song_lists = []

        requests = Request.find_all_by_venue(unique_venue.venue)  
        requests.each do |each_request|
          song_lists << each_request.song
        end
        showdate_hash[unique_venue.showdate] = song_lists

        venue_hash[unique_venue.venue] = showdate_hash
        @venues << venue_hash
      end

Hope you get the idea at least.

Phyo Wai Win
+1  A: 

Here is an efficient solution:

result = Hash.new {|h1, k1| h1[k1] = Hash.new{|h2, k2| h2[k2] = []}}
Request.find_all_by_artist("Metallica", 
 :select => "DISTINCT venue, showdate, LOWER(song) AS song"
).each do |req|
  result[req.venue][req.showdate] << req.song.titlecase
end
KandadaBoggu