使用Groupdate gem每小时进行嵌套分组

时间:2018-08-02 07:39:41

标签: ruby-on-rails

我需要根据以下内容创建一个将Report模型分组的数组:

Mon =>
   00 => 3
   05 => 5
   ...
Tue =>
   04 => 10
   08 =>3
   ...
...

我正在尝试使用Groupdate gem,虽然我可以按一天中的一个小时进行分组,但是给我:

Report.created_in_last(1.year).group_by_hour_of_day(:created_at).count
00 => 3
04 => 10
05 => 5
08 => 3

我也无法将其分组为一周中的几天。是否可以链接Groupdate命令来实现此目的,还是我需要根据一天中的小时将其分组并遍历结果集?

谢谢

1 个答案:

答案 0 :(得分:0)

我从未使用过groupdate,但是根据文档和一些猜测,这是我尝试生成数据的尝试:

Report.created_in_last(1.year)
      .group_by_day_of_week(:created_at, format: '%a')
      .each_with_object({}) do |(weekday, records), out|
        out[weekday] = records.group_by_hour_of_day(:created_at).count
      end