Problem with named_scope using a count

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

On Sat, Jul 24, 2010 at 13:23, Greg Ma [email protected] 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 | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net

Dave A. wrote:

On Sat, Jul 24, 2010 at 13:23, Greg Ma [email protected] 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.

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

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.

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

On Tue, Jul 27, 2010 at 05:36, pepe [email protected] 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 | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs