Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails中创建的_at字段上与group_by合作_Ruby On Rails_Date_Group By - Fatal编程技术网

Ruby on rails 在RubyonRails中创建的_at字段上与group_by合作

Ruby on rails 在RubyonRails中创建的_at字段上与group_by合作,ruby-on-rails,date,group-by,Ruby On Rails,Date,Group By,很抱歉,我找不到一个更轻松的标题 问题是我需要按日期分组,但不是按时间。也就是说,我希望它们按天分组,当按字段中的创建单位分组时会出现问题,因为它会达到秒精度: @stuff = SomeCoolStuff.where(cool_guy_id: @guy.id).group_by(&:created_at) 这给了我这种散列键:2012-10-02 08:16:13+0200,但我想要的键只是2012-10-02 有什么想法吗 提前感谢。您可以在config/locales/en.ym

很抱歉,我找不到一个更轻松的标题

问题是我需要按日期分组,但不是按时间。也就是说,我希望它们按天分组,当按字段中的创建单位分组时会出现问题,因为它会达到秒精度:

@stuff = SomeCoolStuff.where(cool_guy_id: @guy.id).group_by(&:created_at)
这给了我这种散列键:
2012-10-02 08:16:13+0200
,但我想要的键只是
2012-10-02

有什么想法吗


提前感谢。

您可以在
config/locales/en.yml
中尝试以下格式:

en:
  date:
    formats:
      default: "%Y-%m-%d"

您可以在
config/locales/en.yml
中尝试如下格式:

en:
  date:
    formats:
      default: "%Y-%m-%d"

因此,我最终自己找到了答案,因为无论谁对这件事感兴趣,我都会这样做:

@stuff = SomeCoolStuff.where(cool_guy_id: @guy.id).group_by { |stuff| stuff.created_at.to_date }

因此,我最终自己找到了答案,因为无论谁对这件事感兴趣,我都会这样做:

@stuff = SomeCoolStuff.where(cool_guy_id: @guy.id).group_by { |stuff| stuff.created_at.to_date }