Re: [Rails] Problem with named_scope using a count

0 views
Skip to first unread message

Dave Aronson

unread,
Jul 26, 2010, 10:01:31 AM7/26/10
to rubyonra...@googlegroups.com
On Sat, Jul 24, 2010 at 13:23, Greg Ma <li...@ruby-forum.com> 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

Greg Ma

unread,
Jul 27, 2010, 3:29:45 AM7/27/10
to rubyonra...@googlegroups.com
Dave Aronson wrote:
> On Sat, Jul 24, 2010 at 13:23, Greg Ma <li...@ruby-forum.com> 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.
--
Posted via http://www.ruby-forum.com/.

pepe

unread,
Jul 27, 2010, 5:36:07 AM7/27/10
to Ruby on Rails: Talk
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.

Greg Ma

unread,
Jul 27, 2010, 6:07:10 AM7/27/10
to rubyonra...@googlegroups.com
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

Dave Aronson

unread,
Jul 27, 2010, 11:49:40 AM7/27/10
to rubyonra...@googlegroups.com
On Tue, Jul 27, 2010 at 05:36, pepe <Pe...@betterrpg.com> 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.

pepe

unread,
Jul 28, 2010, 5:56:54 AM7/28/10
to Ruby on Rails: Talk
> 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).
Reply all
Reply to author
Forward
0 new messages