# Displaying Calculation on Index

**URL:** <https://www.ruby-forum.com/t/displaying-calculation-on-index/62865>\
**Category:** Rails\
**Created:** [June 8, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865 "2006-06-08T21:12:24Z")\
**Posts on this page:** 6\
**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 8, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865/1 "2006-06-08T21:12:24Z")

</div>

Hi, I am trying to create a simple point system and display the total on  
my index page. So I have my db basically setup like this:

Posts  
id, body, created\_at, user\_id

Post\_points  
id, post\_id, user\_id, created\_at, value

So as you can see I am allowing other users to create points on the post  
and my db will store who gave the point as well. “value” is the point  
value which can either be -1 or 1. Basically I just want to add these  
points together for a given post\_id and display this value on my index.  
Is this possible? If so, could you break the process down for me?

Thanks for your help in advance.

---

<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 8, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865/2 "2006-06-08T21:40:54Z")

</div>

Jordan I. wrote:

> Hi, I am trying to create a simple point system and display the total on  
> my index page. So I have my db basically setup like this:
> 
> Posts  
> id, body, created\_at, user\_id
> 
> Post\_points  
> id, post\_id, user\_id, created\_at, value
> 
> So as you can see I am allowing other users to create points on the post  
> and my db will store who gave the point as well. “value” is the point  
> value which can either be -1 or 1. Basically I just want to add these  
> points together for a given post\_id and display this value on my index.  
> Is this possible? If so, could you break the process down for me?
> 
> Thanks for your help in advance.

should work but untested:

#controller  
points = PostPoint.find(:all,  
:conditions =\> [‘post\_id = ?’, params[:id]])  
@total = points.collect(&:value).inject {|sum, value| sum + value}

#view  
Points: \<%= @total %\>

Or as a model method, which is probably the better way to go:

#model  
def total\_points  
post\_points.collect(&:value).inject do |sum, value|  
sum + value  
end  
end

---

<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 8, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865/3 "2006-06-08T22:32:00Z")

</div>

On Thursday, June 08, 2006, at 9:40 PM, Alex W. wrote:

> > So as you can see I am allowing other users to create points on the post  
> > points = PostPoint.find(:all,  
> > post\_points.collect(&:value).inject do |sum, value|  
> > sum + value  
> > end  
> > end
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Or using AR Calculations…

points = PostPoint.sum(:conditions=\>[[‘post\_id = ?’, params[:id]])

\_Kevin

---

<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 9, 2006, 1:51am UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865/4 "2006-06-09T01:51:27Z")

</div>

Thanks for the replies. Is this possible to get params[:id] on the  
index page? I am not passing any params to load this page. Or maybe I  
should ask, how is it possible to pass the id on when I am just listing  
my posts? because in my view i just have a loop to read all posts…

Any suggestions?

Jordan

Alex W. wrote:

> Jordan I. wrote:
> 
> > Hi, I am trying to create a simple point system and display the total on  
> > my index page. So I have my db basically setup like this:
> > 
> > Posts  
> > id, body, created\_at, user\_id
> > 
> > Post\_points  
> > id, post\_id, user\_id, created\_at, value
> > 
> > So as you can see I am allowing other users to create points on the post  
> > and my db will store who gave the point as well. “value” is the point  
> > value which can either be -1 or 1. Basically I just want to add these  
> > points together for a given post\_id and display this value on my index.  
> > Is this possible? If so, could you break the process down for me?
> > 
> > Thanks for your help in advance.
> 
> should work but untested:
> 
> #controller  
> points = PostPoint.find(:all,  
> :conditions =\> [‘post\_id = ?’, params[:id]])  
> @total = points.collect(&:value).inject {|sum, value| sum + value}
> 
> #view  
> Points: \<%= @total %\>
> 
> Or as a model method, which is probably the better way to go:
> 
> #model  
> def total\_points  
> post\_points.collect(&:value).inject do |sum, value|  
> sum + value  
> end  
> end

---

<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 12, 2006, 2:55am UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865/5 "2006-06-12T02:55:15Z")

</div>

Bump. Anybody have any recommendations for this? Sorry I just can’t  
seem to figure this out…

Thanks in advance!

Jordan I. wrote:

> Thanks for the replies. Is this possible to get params[:id] on the  
> index page? I am not passing any params to load this page. Or maybe I  
> should ask, how is it possible to pass the id on when I am just listing  
> my posts? because in my view i just have a loop to read all posts…
> 
> Any suggestions?
> 
> Jordan

---

<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 22, 2006, 2:14am UTC](https://www.ruby-forum.com/t/displaying-calculation-on-index/62865/6 "2006-06-22T02:14:08Z")

</div>

Slight variation on the same question.

Same database set-up, but with one additional field (headline) in the  
Posts table.

Posts  
id, headline, body, created\_at, user\_id

Post\_points  
id, post\_id, user\_id, created\_at, value

What if you wanted to display all of the posts in order, based on how  
many points each post had? It might look like:

Headline 1 - 20 points  
Headline 2 - 14 points  
Headline 3 - 13 points  
and so on…

You’d need to sum the values for each post\_id, then display them in  
order. And, based on the post\_ids, you’d need to pull in and display  
the headlines from the Posts table. I’ve been trying to get this basic  
scenario to work, but without luck so far.

Any pointers?
