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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Everaldo Gomes at Feb 12, 2012 at 1:51 am
    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
    On Sat, Feb 11, 2012 at 11:40 PM, edward michaels wrote:

    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

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Everaldo Gomes at Feb 12, 2012 at 2:10 am

    On Sat, Feb 11, 2012 at 11:50 PM, Everaldo Gomes wrote:

    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.



    I hope it works.

    Regards,
    Everaldo

    On Sat, Feb 11, 2012 at 11:40 PM, edward michaels wrote:

    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

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Edward michaels at Feb 13, 2012 at 7:01 am
    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.
    On Feb 11, 8:50 pm, Everaldo Gomes wrote:
    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

    On Sat, Feb 11, 2012 at 11:40 PM, edward michaels wrote:






    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
    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Feb 13, 2012 at 9:25 am

    On 13 February 2012 07:00, edward michaels wrote:
    Thanks, your idea did help.

    if I do:

    date = <some_date>
    reduce = Time.where(:date => date)
    I think you might run into trouble using a class Time as this is
    already a ruby class.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 12, '12 at 1:40a
activeFeb 13, '12 at 9:25a
posts5
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase