Displaying Calculation on Index

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.

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

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/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Or using AR Calculations…

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

_Kevin

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

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

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?