Problem with multiple grouping

3 views
Skip to first unread message

szimek

unread,
Oct 13, 2009, 6:47:34 AM10/13/09
to Ruby Reports
Hi,

I'm trying to create a report with user activity across the last few
months. I'd like to create a table where on X axis (table header)
there'd be month names and on Y axis (first table cell in every row)
would be user name.

Here's what I got right now: http://pastie.org/652718.
The problem is that I don't really know how to diplay all months in
the table header without creating another grouping by
['created_on_month'].

Additionally I'd like to print total activity across these months -
sum of all "subgroups.data.size" for each user i.e. in the second
column (the first column is user name).

Any tips?

Cheers,
Szymek

Andrew France

unread,
Oct 13, 2009, 7:41:49 AM10/13/09
to ruby-r...@googlegroups.com
Hi Szimek,

> Here's what I got right now: http://pastie.org/652718.
> The problem is that I don't really know how to diplay all months in
> the table header without creating another grouping by
> ['created_on_month'].
>

Your code assumes that each user will have entries for the same months?
If that's the case, grabbing the keys from the first subgroup hash
should work?
You could also just:
@months = @table.map {|r| r['created_on_month'] }.sort.uniq

If I'm missing something you'll have to explain to me a bit further!

> Additionally I'd like to print total activity across these months -
> sum of all "subgroups.data.size" for each user i.e. in the second
> column (the first column is user name).
>

Not sure about the difficulty here, don't try to be super-efficient,
just iterate over the data as normal using inject(0). You could also use
the add_column method earlier on @table to calculate it.

Not sure if either of those answers help!

Regards,
Andrew

szimek

unread,
Oct 13, 2009, 8:04:03 AM10/13/09
to Ruby Reports
Thanks, this solves the problem with months and I just found out about
the problem you mentioned :)
The code assumes that each user will have entries for every month,
which is not the case, so iterating over sub groups to display the
table doesn't really work that well.

So what's the possible solution?

szimek

unread,
Oct 13, 2009, 8:16:44 AM10/13/09
to Ruby Reports
Ok, I think I got it.
Having months array I can iterate over months instead of subgroups and
select each subgroup by month. If it exists I can display its
data.size, otherwise just 0.
Reply all
Reply to author
Forward
0 new messages