Search code examples
mysqlruby-on-railsrubyreportingruport

How to Group by Day with Ruport / Ruby on Rails?


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.


Solution

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