# Finding highest ratings with acts\_as\_rateable

**URL:** <https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720>\
**Category:** Rails\
**Created:** [October 11, 2006, 1:59am UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720 "2006-10-11T01:59:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_robot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mr\_robot](https://www.ruby-forum.com/u/mr_robot)\
**Post date:** [October 11, 2006, 1:59am UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720/1 "2006-10-11T01:59:24Z")

</div>

hi  
so i can find and set a Posts rating with the acts\_as\_rateable plugin.  
but how would i go about searching for the highest rated Posts, ie.  
search by rating.  
tx

---

<div class="post-metadata">

**Author:** ![mr\_robot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mr\_robot](https://www.ruby-forum.com/u/mr_robot)\
**Post date:** [October 11, 2006, 9:10am UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720/2 "2006-10-11T09:10:02Z")

</div>

acts\_as\_rateable? Are there plug-ins for everything? Is there an  
acts\_as\_profitable yet?

---

<div class="post-metadata">

**Author:** ![mr\_robot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mr\_robot](https://www.ruby-forum.com/u/mr_robot)\
**Post date:** [October 11, 2006, 9:38am UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720/3 "2006-10-11T09:38:05Z")

</div>

My best idea is to

posts = Post.find(:all)  
sorted\_posts = post.sort\_by { |post| post.rating }.reverse  
highest\_rated\_post = sorted\_posts.first

There’s gotta be a better way though.

-j

---

<div class="post-metadata">

**Author:** ![mr\_robot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mr\_robot](https://www.ruby-forum.com/u/mr_robot)\
**Post date:** [October 11, 2006, 9:50am UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720/4 "2006-10-11T09:50:07Z")

</div>

Well you could iterate through them all one at a time. if  
current\_highest\_post.rating \> next\_post.rating else move on to the next  
one.

---

<div class="post-metadata">

**Author:** ![mr\_robot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mr\_robot](https://www.ruby-forum.com/u/mr_robot)\
**Post date:** [October 11, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720/5 "2006-10-11T12:04:35Z")

</div>

thanks josh. works like a charm.

a general question of database retieval efficiency.  
if my database starts to get really large, how will find(:all) searches  
impact on this. ie. will they start to bottledown my application.  
should i always be restricting finds, like so:  
posts=Post.find(:all, :order =\> ‘visit\_count DESC’, :limit=\>200)

---

<div class="post-metadata">

**Author:** ![mr\_robot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mr\_robot](https://www.ruby-forum.com/u/mr_robot)\
**Post date:** [May 3, 2008, 7:13am UTC](https://www.ruby-forum.com/t/finding-highest-ratings-with-acts-as-rateable/71720/6 "2008-05-03T07:13:21Z")

</div>

look at the sql queries it uses and then write your own that mimics  
them.

On Tue, Oct 10, 2006 at 5:59 PM, mr\_robot
