Array statistics

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.

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.