# Retrieving count of individual items among the table

**URL:** <https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550>\
**Category:** Rails\
**Created:** [June 5, 2009, 3:01am UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550 "2009-06-05T03:01:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 5, 2009, 3:01am UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/1 "2009-06-05T03:01:52Z")

</div>

I have a table that is displaying a list of 5 items. Next to each item I  
want to display the number of times that the item pops up in an  
individual list. Example:

Item1 (4)  
Item2 (2)  
Item3 (1)  
Item4 (7)  
Item5 (3)

Here is my code as is:

Index.rhtml.erb

\<%= table.item1 + ‘’ + @statcount %\>  
\<%= table.item2 + ‘’ + @statcount %\>  
\<%= table.item3 + ‘’ + @statcount %\>  
\<%= table.item4 + ‘’ + @statcount %\>  
\<%= table.item5 + ‘’ + @statcount %\>

* * *

And the code used for counting each item in the controller (here is  
where I’m having trouble)

item\_ids = 1…5  
statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
")  
@statcount = table.count(:all, :conditions =\> statcount\_query).to\_s

* * *

Where I have written BLANK is where I can’t find the right code to  
insert. I need to define/pass \<%= table.item1 %\> into this section, but  
I’m quite lost on how to do it exactly. I’ve tried several methods, but  
they keep returning nothing or the total amount of items in the table.

Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 5, 2009, 5:40am UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/2 "2009-06-05T05:40:17Z")

</div>

Jay C. wrote:  
[…]

> item\_ids = 1..5  
> statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
> ")  
> @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s

Well, first of all, you can forget about the map statement and put the  
array item\_ids straight into :conditions – Rails will understand it.  
But that won’t get you what you need, because it will just return a  
total count of all the items that match _any_ ID. I think you need to  
use :group here.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/3 "2009-06-06T12:58:27Z")

</div>

Marnen Laibow-Koser wrote:

> Jay C. wrote:  
> […]
> 
> > item\_ids = 1..5  
> > statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
> > ")  
> > @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s
> 
> Well, first of all, you can forget about the map statement and put the  
> array item\_ids straight into :conditions – Rails will understand it.  
> But that won’t get you what you need, because it will just return a  
> total count of all the items that match _any_ ID. I think you need to  
> use :group here.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I’ve tried the group method listed below for an individual column, but  
it returns just the column entry for “item1” and not the number of  
occurances or count. All I’m getting for the count is “#”. Perhaps this  
code is outdated? Is the there a problem in my controller?

table.find(:all, :select =\> “item1, count(item1) as occurance”,

> ```
> :group => "item1")
> 
> ```

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/4 "2009-06-06T16:59:55Z")

</div>

Jay C. wrote:

> Marnen Laibow-Koser wrote:
> 
> > Jay C. wrote:  
> > […]
> > 
> > > item\_ids = 1..5  
> > > statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
> > > ")  
> > > @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s
> > 
> > Well, first of all, you can forget about the map statement and put the  
> > array item\_ids straight into :conditions – Rails will understand it.  
> > But that won’t get you what you need, because it will just return a  
> > total count of all the items that match _any_ ID. I think you need to  
> > use :group here.
> > 
> > ## Best,
> > 
> > Marnen Laibow-Koser  
> > [http://www.marnen.org](http://www.marnen.org)  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> I’ve tried the group method listed below for an individual column, but  
> it returns just the column entry for “item1” and not the number of  
> occurances or count. All I’m getting for the count is “#”. Perhaps this  
> code is outdated? Is the there a problem in my controller?
> 
> table.find(:all, :select =\> “item1, count(item1) as occurance”,
> 
> > ```
> > :group => "item1")
> > 
> > ```

Still no luck. Is there a way to reference \<%= table.item1 %\> where  
BLANK is in the following code?:

item\_ids = 1..5  
statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
")  
@statcount = table.count(:all, :conditions =\> statcount\_query).to\_s

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/5 "2009-06-06T18:05:14Z")

</div>

Frederick C. wrote:

> On Jun 6, 3:59ï¿½pm, Jay C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Still no luck. Is there a way to reference \<%= table.item1 %\> where  
> > BLANK is in the following code?:
> > 
> > item\_ids = 1..5  
> > ï¿½ ï¿½ statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
> > ")  
> > ï¿½ ï¿½ @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s
> 
> What does your data look like ? count will always return either a  
> single number or a hash of values to counts (if you’ve used a :group  
> option) (to be quite precise, an instance of  
> ActiveSupport::OrderedHash which depending on the rails version you  
> have is backed by a different class).
> 
> Fred

The data is simply a database of lists. For example, the list  
(“grocery”) that contains: “title”, “item1”, “item2”, “item3”, "item4,  
“item5”. It was built behind a scaffold. So the index.rhtml shows all of  
my lists. Ok, no problem there. Here is example output:

“Grocery List”  
Apples  
Pears  
Oranges  
Blah..  
Blah..

and the list output is coded in the index of course as :  
\<%= title.grocery %\>  
\<%= item1.grocery %\>  
\<%= item2.grocery %\>  
etc.  
etc.

* * *

The format that I need to achieve is the same list of items but with  
counts next to them. So below, I can see that Apples appear in 4  
different lists (including this one) while Oranges appear in 5 lists.

“Grocery List”  
Apples 4  
Pears 1  
Oranges 5  
Blah.. 8  
Blah.. 10

* * *

Hopefully that gives an idea of what I’m trying to do. The closest that  
I’ve come to code wise is the below, but I don’t know how to connect the  
table output to it or if there is totally another method for  
accomplishing the same thing.

item\_ids = 1..5  
statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
")  
@statcount = table.count(:all, :conditions =\> statcount\_query).to\_s

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/6 "2009-06-06T18:18:27Z")

</div>

On Jun 6, 5:05 pm, Jay C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s  
> > > (“grocery”) that contains: “title”, “item1”, “item2”, “item3”, "item4,  
> > > “item5”. It was built behind a scaffold. So the index.rhtml shows all of  
> > > my lists. Ok, no problem there. Here is example output:

What I meant by that is what is the structure of your data (columns,  
etables etc.)

Fred

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/7 "2009-06-06T18:38:37Z")

</div>

Frederick C. wrote:

> On Jun 6, 5:05ï¿½pm, Jay C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > > @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s  
> > > > (“grocery”) that ï¿½contains: “title”, “item1”, “item2”, “item3”, "item4,  
> > > > “item5”. It was built behind a scaffold. So the index.rhtml shows all of  
> > > > my lists. Ok, no problem there. Here is example output:
> 
> What I meant by that is what is the structure of your data (columns,  
> etables etc.)
> 
> Fred

Here is the format of index.rhtml and database layout:

 \<% @listhubs.in\_groups\_of(3, false) do |row\_listhubs| %\> \<% for listhub in row\_listhubs %\> 

\<% end %\>

\<% end %\>

| \<%= listhub.title + (link\_to 'edit', edit\_listhub\_path(listhub) + '  
' + listhub.item1 + '  
' + listhub.item2 + '  
' + listhub.item3+ '  
' + listhub.item4+ '  
' + listhub.item5+ '  
' + listhub.item6 + '  
' + listhub.item7 + '  
' + listhub.item8 + '  
' + listhub.item9 + '  
' + listhub.item10 %\> |

* * *

class CreateListhubs \< ActiveRecord::Migration  
def self.up  
create\_table :listhubs do |t|  
t.column :title, :string  
t.column :item1, :string  
t.column :item2, :string  
t.column :item3, :string  
t.column :item4, :string  
t.column :item5, :string  
t.column :item6, :string  
t.column :item7, :string  
t.column :item8, :string  
t.column :item9, :string  
t.column :item10, :string

```
end

```

end

def self.down  
drop\_table :listhubs  
end  
end

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/8 "2009-06-06T17:12:09Z")

</div>

On Jun 6, 3:59 pm, Jay C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Still no luck. Is there a way to reference \<%= table.item1 %\> where  
> BLANK is in the following code?:
> 
> item\_ids = 1..5  
> statcount\_query = item\_ids.map{|id| "item#{id} = BLANK “}.join(” OR  
> ")  
> @statcount = table.count(:all, :conditions =\> statcount\_query).to\_s

What does your data look like ? count will always return either a  
single number or a hash of values to counts (if you’ve used a :group  
option) (to be quite precise, an instance of  
ActiveSupport::OrderedHash which depending on the rails version you  
have is backed by a different class).

Fred

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 6, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/9 "2009-06-06T23:41:04Z")

</div>

On Jun 6, 5:38 pm, Jay C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ‘  
> ’ + listhub.item2 + ‘  
> ’ + listhub.item3+ ‘  
> ’ + listhub.item4+  
> \<% end %\>  
> t.column :item2, :string  
> t.column :item3, :string  
> t.column :item4, :string  
> t.column :item5, :string  
> t.column :item6, :string  
> t.column :item7, :string  
> t.column :item8, :string  
> t.column :item9, :string  
> t.column :item10, :string

ah, so a list having 3 apples is represented by 3 of the columns  
having value ‘apple’ ? That’s an unusual way of organising your data,  
and doing that sort of counting in sql is a nightmare. I would do  
things like this:

class Listhub  
def values  
[item1, item2, item3, item4, item5, item6, item7, item8, item9,  
item10].compact  
end  
end

then look at values. You can use group\_by to group identical items, ie

values.group\_by {|value| value}

will return values like {“apple”=\>[“apple”, “apple”, “apple”],  
“banana”=\>[“banana”], “pear”=\>[“pear”, “pear”]}

and then it is simple enough to use map on that to get counts for each  
key, eg

values.group\_by {|v| v}.map {|key, items|[key, items.length]}

#=\> [[“apple”, 3], [“banana”, 1], [“pear”, 2]] (assuming that values  
evaluated to [“apple”, “pear”, “apple”, “apple”, “banana”, “pear”])

Fred

---

<div class="post-metadata">

**Author:** ![jayco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jayco](https://www.ruby-forum.com/u/jayco)\
**Post date:** [June 7, 2009, 8:44am UTC](https://www.ruby-forum.com/t/retrieving-count-of-individual-items-among-the-table/169550/10 "2009-06-07T08:44:35Z")

</div>

2009/6/6 Jay C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â Â t.column :item5, :string  
> Â Â Â t.column :item6, :string  
> Â Â Â t.column :item7, :string  
> Â Â Â t.column :item8, :string  
> Â Â Â t.column :item9, :string  
> Â Â Â t.column :item10, :string

Have you considered having a separate table of list\_items with a HABTM  
relationship between the listhub and the list\_items? Alternatively if  
an Apple on one list should be a different object to one on another  
(in other words are the list\_items objects in their own right or are  
they object types) then listhub has\_many list\_items and list\_item  
belongs\_to listub. These may appear to be more complex solutions but  
in the long run might prove easier. Your counting for example would  
be much easier.

Colin
