如何使用 Ruport / Ruby on Rails 按天分组?

发布于 2024-08-04 13:06:38 字数 133 浏览 4 评论 0原文

我正在尝试评估 Ruport 在我的 Rails 应用程序中的使用,但不确定如何获取一系列带有日期/时间戳的记录并通过 Ruport 的分组功能对它们进行分组。

如果 Ruport 没有意义,我愿意接受其他/更好的方法来进行相同的分组。

I'm trying to evaluate Ruport for use in my Rails app, but am not sure how to take a series of records with date/time stamps and group them via Ruport's grouping functions.

I'm open to other/better methods to do this same grouping if Ruport doesn't make sense.

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

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

发布评论

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

评论(3

舂唻埖巳落 2024-08-11 13:06:38

不使用 ruport:

<% @posts.group_by {|p| p.created_at.at_beginning_of_day }.each do |day, posts| %>
  <h2><%= day.strftime("something...") %></h2>

  <% posts.each do |post| %>
    do stuff with `post`.
  <% end %>
<% end %>

使用 at_beginning_of_monthat_beginning_of_week 等,具体取决于您想要分组的依据。

Without ruport:

<% @posts.group_by {|p| p.created_at.at_beginning_of_day }.each do |day, posts| %>
  <h2><%= day.strftime("something...") %></h2>

  <% posts.each do |post| %>
    do stuff with `post`.
  <% end %>
<% end %>

Use at_beginning_of_month, at_beginning_of_week and so on, depending on what you want to group by.

泛泛之交 2024-08-11 13:06:38

Ruport 包括一种向表添加新列的方法。因此,可以像这样完成时间戳源的日期明智分组:

@table.add_column('created_at_date') { |r| r.created_at.to_date }
created_at_date_grouping = Grouping(@table, :by => "created_at_date")

当然,此方法也可以用于生成更有趣的报告,例如 Google Analytics:

@table.add_column('day_of_week') { |r| r.created_at.strftime('%a') }
@table.add_column('hour_of_day') { |r| r.created_at.strftime('%H') }

但不知道性能。

Ruport includes a method to add a new column to a table. So date wise grouping from a timestamp source can be done like this:

@table.add_column('created_at_date') { |r| r.created_at.to_date }
created_at_date_grouping = Grouping(@table, :by => "created_at_date")

This method can, of course, also be used to generate more interesting reports like on Google Analytics:

@table.add_column('day_of_week') { |r| r.created_at.strftime('%a') }
@table.add_column('hour_of_day') { |r| r.created_at.strftime('%H') }

Don't know about performance though.

如果没有 2024-08-11 13:06:38

我建议将 @posts.group_by 函数移到控制器而不是视图中。

I would suggest moving the @posts.group_by function into the controller rather than the view.

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