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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Jul 24, 2010 at 13:23, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson wrote:> On Sat, Jul 24, 2010 at 13:23, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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.... > > -DaveSorry 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/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Jul 27, 3:29 am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Dave Aronson wrote: > > On Sat, Jul 24, 2010 at 13:23, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Jul 27, 2010 at 05:36, pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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). -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.