# Problem with named\_scope using a count

**URL:** <https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139>\
**Category:** Rails\
**Created:** [July 24, 2010, 7:23pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139 "2010-07-24T19:23:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 24, 2010, 7:23pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/1 "2010-07-24T19:23:19Z")

</div>

Hi,  
I have two models, Post and Rate. You can rate each post with “good” or  
“bad”.  
I would like to create a named\_scope to get the best posts.  
How can I do that?

named\_scope :top, lambdaÂ {  
{  
???  
}  
}

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 26, 2010, 4:05pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/2 "2010-07-26T16:05:18Z")

</div>

On Sat, Jul 24, 2010 at 13:23, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have two models, Post and Rate. You can rate each post with “good” or  
> “bad”.  
> I would like to create a named\_scope to get the best posts.  
> How can I do that?

First you need to define “best”. What is the overall rating for each  
post? Good minus bad? Total good and ignore the bad? Something more  
complex? How many “best” do you want?

Once you’ve defined that, you can figure out how to do it  
programmatically for each post. Offhand I’d say this might wind up  
being too slow to do on the fly for all posts, so as to find the top.  
Premature optimization is the root of all evil, but still I’d give  
SOME thought to how to stash a post’s overall rating, especially in a  
way that will make it trivial to retrieve the top N “best” posts (or  
“worst” for that matter). Further thoughts if you like…

-Dave

–  
Specialization is for insects. -RAH | Have Pun, Will Babble! -me  
Programming Blog: [http://codosaur.us](http://codosaur.us) | Work: [http://davearonson.com](http://davearonson.com)  
Leadership Blog: [http://dare2xl.com](http://dare2xl.com) | Play: [http://davearonson.net](http://davearonson.net)

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 27, 2010, 9:29am UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/3 "2010-07-27T09:29:45Z")

</div>

Dave A. wrote:

> On Sat, Jul 24, 2010 at 13:23, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have two models, Post and Rate. You can rate each post with “good” or  
> > “bad”.  
> > I would like to create a named\_scope to get the best posts.  
> > How can I do that?
> 
> First you need to define “best”. What is the overall rating for each  
> post? Good minus bad? Total good and ignore the bad? Something more  
> complex? How many “best” do you want?
> 
> Once you’ve defined that, you can figure out how to do it  
> programmatically for each post. Offhand I’d say this might wind up  
> being too slow to do on the fly for all posts, so as to find the top.  
> Premature optimization is the root of all evil, but still I’d give  
> SOME thought to how to stash a post’s overall rating, especially in a  
> way that will make it trivial to retrieve the top N “best” posts (or  
> “worst” for that matter). Further thoughts if you like…
> 
> -Dave

Sorry I haven’t been very clear.  
The model Rate has a column kind where it is stored “good” or “bad”.  
What I want is to count all the “good” and “bast” rates.

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 27, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/4 "2010-07-27T12:07:10Z")

</div>

pepe wrote:

> Maybe adding 2 integer columns to your Posts table to count the hits?
> 
> When somebody rates the Post you just add 1 to whichever column you  
> need. Then it would be a simple matter of sorting your posts in  
> ascending/descending sequence by the column you want to work with.
> 
> I would add indexes by the columns to make it work faster.

Good idea

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 27, 2010, 11:39am UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/5 "2010-07-27T11:39:34Z")

</div>

Maybe adding 2 integer columns to your Posts table to count the hits?

When somebody rates the Post you just add 1 to whichever column you  
need. Then it would be a simple matter of sorting your posts in  
ascending/descending sequence by the column you want to work with.

I would add indexes by the columns to make it work faster.

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 28, 2010, 11:58am UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/6 "2010-07-28T11:58:24Z")

</div>

> That’s pretty much what I had in mind for stashing. There is some  
> merit to still having a separate Rate model, storing _who_ rated each  
> Post as good or bad (and maybe when?), to prevent ballot-stuffing and  
> let them change it later. However, I would be tempted to call it  
> Rating, since the noun Rate is how often something happens, not an  
> opinon.

I would agree with your naming of the Rating model. About the ballot-  
stuffing you would have to come up with logic to stop it. First you  
would need to be able to uniquely identify a user/visitor/rater and  
then figure out some logic to stop the ballot-stuffing depending on  
your needs (one vote only, or one vote per day/hour/week, etc).

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [July 27, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/problem-with-named-scope-using-a-count/192139/7 "2010-07-27T17:51:53Z")

</div>

On Tue, Jul 27, 2010 at 05:36, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Maybe adding 2 integer columns to your Posts table to count the hits?
> 
> When somebody rates the Post you just add 1 to whichever column you  
> need. Then it would be a simple matter of sorting your posts in  
> ascending/descending sequence by the column you want to work with.
> 
> I would add indexes by the columns to make it work faster.

That’s pretty much what I had in mind for stashing. There is some  
merit to still having a separate Rate model, storing _who_ rated each  
Post as good or bad (and maybe when?), to prevent ballot-stuffing and  
let them change it later. However, I would be tempted to call it  
Rating, since the noun Rate is how often something happens, not an  
opinon.

-Dave

–  
Specialization is for insects. -RAH | Have Pun, Will Babble! -me  
Programming Blog: [http://codosaur.us](http://codosaur.us) | Work: [http://davearonson.com](http://davearonson.com)  
Leadership Blog: [http://dare2xl.com](http://dare2xl.com) | Play: [http://davearonson.net](http://davearonson.net)
