I have an active record Times table with a date column and an hour
column. The hours are unique but the dates can have many hours. How
would I create a hash that mapped a date key to its hours? Something
like:
hours_per_date['2/14/2012'] => ['6:00', '8:00', '11:00', '2:00']
Thanks
Hi!
Take a look at:
http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-group_by
and
http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-collect
I think they could help.
E.g:
hash = Time.all.collect { |t| [ t.date , t.hour ] }.group_by { |date,hour| date.strftime(“%-m/%-d/%Y”) }
I hope it works.
Regards,
Everaldo
Hi!
Take a look at:
http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-group_by
and
http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-collect
I think they could help.
E.g:
hash = Time.all.collect { |t| [ t.date , t.hour ] }.group_by { |date,hour| date.strftime(“%-m/%-d/%Y”) }
I forgot that after using group_by you will have to discard the date from the hash values.
Thanks, your idea did help.
if I do:
date = <some_date>
reduce = Time.where(:date => date)
hour_on_date = reduce.collect {|x| x.hour}
Now I just need to figure out how to make <some_date> a hash key which
triggers the hour_on_date array value.
Colin_Law1
(Colin Law)
February 13, 2012, 9:24am
5
I think you might run into trouble using a class Time as this is
already a ruby class.
Colin