How do I group by day instead of date?

前端 未结 6 1750
情书的邮戳
情书的邮戳 2020-12-03 13:56

ok so i have

 >> list = Request.find_all_by_artist(\"someBand\")
=> [#

        
6条回答
  •  醉梦人生
    2020-12-03 14:44

    Time is a quite complex object to group by. Assuming you want to group by the creation date, instead of the full Time, start creating a custom method in your model to return the group criteria.

    The method should return the creation date, possibly as string.

    def group_by_criteria
      created_at.to_date.to_s(:db)
    end
    

    Then, group by that method.

    list.group_by(&:group_by_criteria).map {|k,v| [k, v.length]}.sort
    

提交回复
热议问题