views:

79

answers:

2

Referencing this post:

http://stackoverflow.com/questions/1272888/ruby-on-rails-average-per-day

I'm getting the average per day, which works fine. Until now that the month switched over...

I have this code

score = (7.days.ago.to_date.to_s(:db)..Date.today.tomorrow.to_s(:db)).map{|dt| [dt, ave_score[dt] || 0]}

Which works, but now that the month switches over, it comes back with dates that don't exist, like 2009-08-32 to 2009-08-99 and 2009-09-00. All in the array. How can I delete dates that don't actually exist.

+1  A: 

Try waiting to make the call to to_s:

score = (7.days.ago.to_date..Date.today.tomorrow).map{ |dt| d = dt.to_s(:db); [d, ave_score[d] || 0] }
Andy Gaskell
A: 

I figured it out, I'm not sure if this is the best way of doing it so if anybody else would like to add something go for it:

arr_count = 0
length = score.length
while arr_count < length
   begin
      score[arr_count][0].to_date  
      arr_count = arr_count + 1
   rescue
     @score.delete_at(arr_count)
     length = length - 1
   end
end
Ryan
What happened when you tried the code I posted?
Andy Gaskell
It probably wasn't tried. People only use things if it was what they wanted to hear.
railsninja