# Array statistics

**URL:** <https://www.ruby-forum.com/t/array-statistics/117299>\
**Category:** Rails\
**Created:** [October 15, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/array-statistics/117299 "2007-10-15T20:28:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [October 15, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/array-statistics/117299/1 "2007-10-15T20:28:33Z")

</div>

I built a view that includes a form\_remote\_tag that will take user  
input and dynamically generate a table list of requested items. The  
user can change the input to display different results.

The data is modeled similar to this

Item  
has a status (column)  
has a group (column)  
has many related other items (lookup table)

There are probably about 10 or more possible combinations of how the  
user could request to return back results. However, each combination  
may not return a result… So for instance, does GROUP A have an item  
in STATUS X where other\_items exist? The user will need to hit each  
combination that will return results so that those items can be  
evaluated. But the problem is, the user shouldn’t need to try every  
combination to find the ones with results.

So what I wanted to do was to display a table before the form that  
says…

Group Name Status Other Items How Many  
A Open True 5  
A Open False 0  
B Working False 2

A simple query returns all results Items.find(:all, :conditions =\>  
xyz). But i need to count how many items for each group that do and  
do not have other items. I dont want to have a bajillion queries to  
the database to count all the possible combinations. I guess my  
question boils down to how can I work with a hashed array to count  
these various combinations?

Sorry if this seems confusing.

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [October 16, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/array-statistics/117299/2 "2007-10-16T20:00:18Z")

</div>

On Oct 15, 2007, at 11:27 AM, predhme wrote:

> A simple query returns all results Items.find(:all, :conditions =\>  
> xyz). But i need to count how many items for each group that do and  
> do not have other items. I dont want to have a bajillion queries to  
> the database to count all the possible combinations.

The database may be more efficient than pure Ruby. Depends on the  
size of the result set. This is the kind of thing databases are good  
at using grouping and one query. I’m sure some keen Ruby solution  
using map or uniq would work, but it depends on iterating the entire  
result set in Ruby whereas the database would do it in compiled C.
