How could I group by time(HH:MM) with mongoid or ruby -
how group time(hh:mm) mongoid or ruby
i want group following data departure_at
(hh:mm).
how easier rails or mongoid
expect result:
{ '06:40': [item125, item131], '10:20': [item126], ....
query result mongoid
[125] #<tiger _id: 811_0640_1010_tpe_kix, to: "kix", from: "tpe", price: 4899,departure_at: 08-11 06:40:00 utc>, [126] #<tiger _id: 811_1020_0305_tpe_kix, to: "kix", from: "tpe", price: 5450,departure_at: 08-11 10:20:00 utc>, .... [131] #<tiger _id: 812_0640_1010_tpe_kix, to: "kix", from: "tpe", price: 4899,departure_at: 08-12 06:40:00 utc>,
first, make array of ["hh:mm", object]
. can use trick this question
array = @your_objects.map{|obj| [obj.date_departure.strftime("%h:%m"), obj]} hash[ array.group_by(&:first).map{ |k,a| [k,a.map(&:last)] } ]
Comments
Post a Comment