Ruby-on-rails 如何按天而不是按日期分组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4987392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:25:04  来源:igfitidea点击:

How do I group by day instead of date?

ruby-on-railsrubyruby-on-rails-3group-by

提问by Matt Elhotiby

ok so i have

好的,所以我有

 >> list = Request.find_all_by_artist("someBand")
=> [#<Request id: 1, artist: "someBand", song: "someSong", venue: "Knebworth - Stevenage, United Kingdom", showdate: "2011-07-01", amount: nil, user_id: 2, created_at: "2011-01-01 18:14:08", updated_at: "2011-01-01 18:14:09".............

and then

进而

list.group_by(&:created_at).map {|k,v| [k, v.length]}.sort
=> [[Sat, 01 Jan 2011 18:14:08 UTC +00:00, 10], [Sun, 09 Jan 2011 18:34:19 UTC +00:00, 1], [Sun, 09 Jan 2011 18:38:48 UTC +00:00, 1], [Sun, 09 Jan 2011 18:51:10 UTC +00:00, 1], [Sun, 09 Jan 2011 18:52:30 UTC +00:00, 1], [Thu, 10 Feb 2011 02:22:08 UTC +00:00, 1], [Thu, 10 Feb 2011 20:02:20 UTC +00:00, 1]]

the problem is I have a few Sun, 09 Jan and a couple for the 10th, instead of one like this

问题是我有几个星期日,1 月 9 日和一对夫妇在 10 日,而不是这样的

this is what i need

这就是我需要的

=> [[Sat, 01 Jan 2011 18:14:08 UTC +00:00, 10], [Sun, 09 Jan 2011 18:34:19 UTC +00:00, 4], [Thu, 10 Feb 2011 20:02:20 UTC +00:00, 2]]

回答by Simone Carletti

Timeis 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.

Time是一个非常复杂的分组对象。假设您想按创建日期而不是 full 进行分组Time,请开始在模型中创建自定义方法以返回组条件。

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

回答by Fellps

I think this is a much more elegant and simple solution

我认为这是一个更优雅和简单的解决方案

list.group_by{|x| x.created_at.strftime("%Y-%m-%d")} 

回答by laffuste

There is a gem for that: groupdate.

有一个宝石:groupdate

Usage (from the docs):

用法(来自文档):

User.group_by_day(:created_at).count
# {
#   2013-04-16 00:00:00 UTC => 50,
#   2013-04-17 00:00:00 UTC => 100,
#   2013-04-18 00:00:00 UTC => 34
# }

回答by you786

Ipsum's answer is actually good and probably the best:

Ipsum 的答案实际上很好,而且可能是最好的:

In Arel:

在阿雷尔:

requests = Arel::Table.new(:requests)
query = requests.project("COUNT(*), CAST(requests.created_at AS DATE) as created_at")
query = query.group("CAST (requests.created_at AS DATE)")
Request.find_by_sql(query.to_sql)

回答by Abel

Group without extra gems:

没有额外宝石的组:

def self.group_by_day items
   data = items.group_by{|x| x.created_at.to_date}
   chart_data = {}

   data.each do |a,b|
     chart_data.merge!({a => b.count})
   end

   return chart_data
end

回答by Ibrahim Saifee

you can use GROUP BY DATE(created_at)in MySQL

你可以GROUP BY DATE(created_at)在 MySQL 中使用

On ruby code you can use like this

在 ruby​​ 代码上,您可以像这样使用

list.group('DATE(created_at)').map {|k,v| [k, v.length]}.sort