如何按天而不是日期分组?

发布于 2024-10-17 08:52:19 字数 971 浏览 5 评论 0原文

好的,我已经了

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

,然后

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]]

问题是我有几个周日,1 月 9 日和几个 10 号,而不是这样的,

这就是我需要的

=> [[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]]

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

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]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

倾城°AllureLove 2024-10-24 08:52:19

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

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

I think this is a much more elegant and simple solution

list.group_by{|x| x.created_at.strftime("%Y-%m-%d")} 
知足的幸福 2024-10-24 08:52:19

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

该方法应该返回创建日期,可能是字符串。

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

然后,按该方法分组。

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

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
乖乖公主 2024-10-24 08:52:19

有一个宝石:groupdate

用法(来自文档):

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
# }

There is a gem for that: 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
# }
扭转时空 2024-10-24 08:52:19

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

在 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)

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

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)
风筝有风,海豚有海 2024-10-24 08:52:19

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

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

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

you can use GROUP BY DATE(created_at) in MySQL

On ruby code you can use like this

list.group('DATE(created_at)').map {|k,v| [k, v.length]}.sort
血之狂魔 2024-10-24 08:52:19

没有额外宝石的组:

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

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
山人契 2024-10-24 08:52:19

我最近发现 groupdate gem 似乎很适合处理此任务。

此代码示例演示了如何在实践中使用它来解决问题:

list.group_by_day(:created_at)

这里最重要的部分是它在幕后生成 SQL,这比计划 Ruby 代码的性能要高得多。

I recently discovered groupdate gem which seems like a good fit for handling this task.

This code example demonstrates how to use it in practice to solve the problem:

list.group_by_day(:created_at)

The most important part here is that behind the scene it generates SQL, that's much more performant than a plan Ruby code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文