Group_by

Hi! I’m hoping somebody may be able to help with my group_by problems
using some information that I imported from Google Analytics.

I’ve been trying to accomplish two things:

  1. Inside the Stat table, I’ve been trying to sum up all pageviews for a
    given month, where the date of the record is stored as YYYY-MM-DD in the
    field named “date”. The current group_by works fine for pulling out the
    individual records by month, but I can’t figure out how to use sum to do
    the same for pageviews over the whole month.

  2. This is probably significantly more difficult, but can you do a
    group_by over multiple fields? Specifically, I wanted to group by
    site_id and then by month.

I hope these requests aren’t too much, but any assistance is
appreciated.

def index
@gatotals = Stat.find(:all)
@ga_months = @gatotals.group_by { |t| t.date.month }
end

index.html.erb

<% @ga_months.sort.each do |month, records| %>
<%= monthname(month) %>
<% for record in records %>
<%= record.date %>
<%= number_with_delimiter(record.pageviews, :separator => “,”) %>
<% end %>
<% end %>