1

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.

Mike Buckbee
  • 6,793
  • 2
  • 33
  • 36

3 Answers3

6

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.

August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
2

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.

palam
  • 21
  • 1
1

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

mike_wags
  • 43
  • 4