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 }