# Calculating row COUNTs

**URL:** <https://www.ruby-forum.com/t/calculating-row-counts/62385>\
**Category:** Rails\
**Created:** [June 3, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385 "2006-06-03T20:25:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 3, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/1 "2006-06-03T20:25:02Z")

</div>

hi everyone,

I’m trying to do a simple calculation in my rails project. I would like  
to find all rows in a table that match the given id of another table  
(ie. comment\_id) and has the value = 1 (“value” is a field in the  
table), then add those values up. Next find the same rows with the  
value = -1 and add those values up also. Finally, I would like to find  
the total. Therefore, if there were 5 rows with value = 1, this would  
be 5 and 3 rows with value = -1, this would be -3. Then when I add them  
together, it would be totalvalue = 2.

Would somebody be able to help me with this? Or is there a better way to  
do this? I’m not exactly sure where and how to setup this (do I do this  
in the model or controller?).

Thanks!

---

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 3, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/2 "2006-06-03T23:21:34Z")

</div>

Add this as a method on the model…

---

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 4, 2006, 1:36am UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/3 "2006-06-04T01:36:10Z")

</div>

You can use activerecord calculations -

Vote.sum(:value, :group =\> :value, :conditions =\> "comment\_id = …

To get the sum of the sums, you could call Vote.sum again without the  
:group  
option, or iterate and sum the result from the first calculation,  
avoiding  
another pass at the database.

The method might make most sense on the Comment model. The Comment  
could  
set a scope (with\_scope) on the Vote model, and then call sum without  
conditions. You might be more interested in calling the count and  
average  
calculations instead of the sum calculation, if I am guessing correctly  
that  
you are trying to report the total number of votes, and the average  
value of  
the vote.

---

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 4, 2006, 8:03am UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/4 "2006-06-04T08:03:17Z")

</div>

> def show  
> @blah = Blah.find(params[:id])  
> @totalpoints = Blah.find(params[:id]).blah\_points.sum(‘value’,  
> :conditions =\> ‘blah\_id = ???’)  
> end
> 
> I would like ‘blah\_id’ equal to the params[:id].

With ActiveRecord you can insert custom values in conditions – that  
will be automatically quoted for you – by using an Array where the  
first element is the condition string with question marks as values,  
and other elements as the values to replace those question marks. In  
your case, that would be :

@totalpoints = Blah.find(params[:id]).blah\_points.sum  
(‘value’, :conditions =\> [‘blah\_id = ?’, params[:id]])

Roman

---

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 4, 2006, 8:15am UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/5 "2006-06-04T08:15:28Z")

</div>

ahh! It worked perfectly! That is great information. I always wondered  
what the ‘?’ meant.

Thanks to all whom contributed!

Roman LE NEGRATE wrote:

> > def show  
> > @blah = Blah.find(params[:id])  
> > @totalpoints = Blah.find(params[:id]).blah\_points.sum(‘value’,  
> > :conditions =\> ‘blah\_id = ???’)  
> > end
> > 
> > I would like ‘blah\_id’ equal to the params[:id].
> 
> With ActiveRecord you can insert custom values in conditions – that  
> will be automatically quoted for you – by using an Array where the  
> first element is the condition string with question marks as values,  
> and other elements as the values to replace those question marks. In  
> your case, that would be :
> 
> @totalpoints = Blah.find(params[:id]).blah\_points.sum  
> (‘value’, :conditions =\> [‘blah\_id = ?’, params[:id]])
> 
> Roman

---

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 4, 2006, 7:50am UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/6 "2006-06-04T07:50:16Z")

</div>

Thank you so much for your reply! This helped me out greatly. Now I am  
almost there! Does anyone know how to call the ID within the conditions?

Here is what I have:

def show  
@blah = Blah.find(params[:id])  
@totalpoints = Blah.find(params[:id]).blah\_points.sum(‘value’,  
:conditions =\> ‘blah\_id = ???’)  
end

I would like ‘blah\_id’ equal to the params[:id].

BTW: For archieve reference, I ended up using the sum() in activeRecord  
to do this calculations:  
[http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000827](http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000827)

Thanks again!  
Jordan

Vince Nibler wrote:

> You can use activerecord calculations -
> 
> Vote.sum(:value, :group =\> :value, :conditions =\> "comment\_id = …
> 
> To get the sum of the sums, you could call Vote.sum again without the  
> :group  
> option, or iterate and sum the result from the first calculation,  
> avoiding  
> another pass at the database.
> 
> The method might make most sense on the Comment model. The Comment  
> could  
> set a scope (with\_scope) on the Vote model, and then call sum without  
> conditions. You might be more interested in calling the count and  
> average  
> calculations instead of the sum calculation, if I am guessing correctly  
> that  
> you are trying to report the total number of votes, and the average  
> value of  
> the vote.

---

<div class="post-metadata">

**Author:** ![jtei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jtei](https://www.ruby-forum.com/u/jtei)\
**Post date:** [June 4, 2006, 8:31am UTC](https://www.ruby-forum.com/t/calculating-row-counts/62385/7 "2006-06-04T08:31:06Z")

</div>

Oops, one more question: is it possible at all to display these points  
in my index view (ie. no id supplied). Since this calculation is not  
stored in the database, I wouldn’t be able to access it in the view  
right? or is it possible to access this value in the loop (for blah in  
@blahs) somehow?? Or for this, do I have to create a new model? - I  
think the shopping cart example does this in the book.

Thanks again!

Jordan I. wrote:

> ahh! It worked perfectly! That is great information. I always wondered  
> what the ‘?’ meant.
> 
> Thanks to all whom contributed!
> 
> Roman LE NEGRATE wrote:
> 
> > > def show  
> > > @blah = Blah.find(params[:id])  
> > > @totalpoints = Blah.find(params[:id]).blah\_points.sum(‘value’,  
> > > :conditions =\> ‘blah\_id = ???’)  
> > > end
> > > 
> > > I would like ‘blah\_id’ equal to the params[:id].
> > 
> > With ActiveRecord you can insert custom values in conditions – that  
> > will be automatically quoted for you – by using an Array where the  
> > first element is the condition string with question marks as values,  
> > and other elements as the values to replace those question marks. In  
> > your case, that would be :
> > 
> > @totalpoints = Blah.find(params[:id]).blah\_points.sum  
> > (‘value’, :conditions =\> [‘blah\_id = ?’, params[:id]])
> > 
> > Roman
