# Calculating average ratings and other imponderables

**URL:** <https://www.ruby-forum.com/t/calculating-average-ratings-and-other-imponderables/101205>\
**Category:** Rails\
**Created:** [August 3, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/calculating-average-ratings-and-other-imponderables/101205 "2006-08-03T15:24:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfharlock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@pfharlock](https://www.ruby-forum.com/u/pfharlock)\
**Post date:** [August 3, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/calculating-average-ratings-and-other-imponderables/101205/1 "2006-08-03T15:24:58Z")

</div>

Okay, this _should_ be easy, but it isn’t, although I reserve the right  
to  
smack myself upside the head if it is:

Assume that a smart has\_many ratings, and a rating belongs\_to a smart  
(the  
models are fine). Each rating has a column smart\_id.

Each smart, then, has many ratings, with a column called “rating” that  
is  
an int (1-10).

I want to find, say, the ten smarts with the highest average rating.

Calculating an average is easy enough–once I know which smart I’m  
looking  
at. But how do I find this on the fly?

Anyone? Anyone? Bueller? Bueller?

---

<div class="post-metadata">

**Author:** ![pfharlock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@pfharlock](https://www.ruby-forum.com/u/pfharlock)\
**Post date:** [August 3, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/calculating-average-ratings-and-other-imponderables/101205/2 "2006-08-03T16:25:07Z")

</div>

On 8/3/06, Gary Steven W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Okay, this _should_ be easy, but it isn’t, although I reserve the right to  
> smack myself upside the head if it is:
> 
> Assume that a smart has\_many ratings, and a rating belongs\_to a smart (the  
> models are fine). Each rating has a column smart\_id.
> 
> Each smart, then, has many ratings, with a column called “rating” that is  
> an int (1-10).
> 
> I want to find, say, the ten smarts with the highest average rating.

Not sure if this works (SQL isn’t my strong suit!) but:

select smart\_id as id, sum(rating)/count(\*) as average from ratings  
group by smart\_id order by average limit 10;

If not it might point you in the right direction.

Matt

---

<div class="post-metadata">

**Author:** ![pfharlock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@pfharlock](https://www.ruby-forum.com/u/pfharlock)\
**Post date:** [August 3, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/calculating-average-ratings-and-other-imponderables/101205/3 "2006-08-03T16:52:17Z")

</div>

Gary wrote:

> I want to find, say, the ten smarts with the highest average rating.
> 
> Calculating an average is easy enough–once I know which  
> smart I’m looking at. But how do I find this on the fly?

You have to calculate all averages before you can determine which are  
the highest.

Doing it in sql would be fastest, but maybe something like this? (DIRE  
WARNING: Utter newbie who hasn’t yet written a Rails app, so take this  
as pseudocode as it may not work as is)

def top\_ten  
average = Hash.new

Smart.find(:all).each {|smart|  
r = smart.ratings  
r.inject(0) {|sum, i| sum + i}  
average[smart.id] = sum/r.size  
}

top = average.keys.sort\_by {|x| average[x]}

# Return top 10 smart ids

top[-10…top.size]  
end

Hopefully this will get you going (and I’d like to know what parts of  
the above are wrong for my own learning purposes).

- Mark.
