Ok so this may seem simple but I don’t even know what to do here. This
seems like some sort of “ruby math” maybe? Ok so here is what I want to
do: I want users to submit a user rating from 1-10. How about a
drop-down box? Then I want to submit the data into the db. ok so I can
do all that. Now here is the thing. I have a bunch of “ratings”. Each
was an id, a game_id, and a rating. First off, do I need any other
fields in the db? And second, how do I get an “average user rating”.
Maybe something like add up all the numbers, and divide them by the
number of ratings for that game_id??? idk. Any help would be great =)
Jacob J. wrote:
Ok so this may seem simple but I don’t even know what to do here. This
seems like some sort of “ruby math” maybe? Ok so here is what I want to
do: I want users to submit a user rating from 1-10. How about a
drop-down box? Then I want to submit the data into the db. ok so I can
do all that. Now here is the thing. I have a bunch of “ratings”. Each
was an id, a game_id, and a rating. First off, do I need any other
fields in the db? And second, how do I get an “average user rating”.
Maybe something like add up all the numbers, and divide them by the
number of ratings for that game_id??? idk. Any help would be great =)
See:
ActiveRecord::Calculations::ClassMethods
James B. wrote:
See:
ActiveRecord::Calculations::ClassMethods
Sorry about that. Pushed the button too soon.
Consider a table called “ratings” with columns “user_id” “game_id” &
“rating”
Game.average :rating, :conditions => [‘game_id = ?’, 21]
gives average for game_id 21
Game.average :rating
gives average for all rows
Game.count :conditions => [‘rating > ?’, 5.5]
gives total number of ratings gt 5.5
etc.
Most, nay all, relational database systems have basic tools like
average, min, max, and count built right in, so ActiveRecord will
usually (always) have a simple way to get at them.
James B. wrote:
James B. wrote:
See:
ActiveRecord::Calculations::ClassMethods
Sorry about that. Pushed the button too soon.
Consider a table called “ratings” with columns “user_id” “game_id” &
“rating”
Table name should be “games” for the examples that I gave or else
“Game.” should be read as “Rating.” in all instances. Must go to bed…
James B. wrote:
James B. wrote:
James B. wrote:
See:
ActiveRecord::Calculations::ClassMethods
Sorry about that. Pushed the button too soon.
Consider a table called “ratings” with columns “user_id” “game_id” &
“rating”
Table name should be “games” for the examples that I gave or else
“Game.” should be read as “Rating.” in all instances. Must go to bed…
wow this whole thing is great. Thanks so much. It seems simple, I’ve
just never really done anything in this part of rails yet. Thanks a
bunch for the detailed help. I should be able to find the rest of the
info in the API =)