Hi, Let''s say that in my "users" table I have a column called "points" that is indexed. I would like to be able to query the position of any given user within that index. In other words, I want the same data that the following method would give: def rank result = 0 User.find(:all).each {|u| result=result+1 if u.points > points} result end However I can''t use the above method because the user database has a quarter of a million records and is growing. Help! and Thanks in advance. Ben --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 27, 2007, at 3:42 PM, Ben Nevile wrote:> Hi, > > Let''s say that in my "users" table I have a column called "points" > that is indexed. I would like to be able to query the position of any > given user within that index. In other words, I want the same data > that the following method would give: > > def rank > result = 0 > User.find(:all).each {|u| result=result+1 if u.points > points} > result > end > > However I can''t use the above method because the user database has a > quarter of a million records and is growing. > > Help! and Thanks in advance. > Benclass User def self.having_more_points_than points count(:conditions => [ ''points > ?'', points ]) end def rank self.class.having_more_points_than(self.points) + 1 end end You might want to save the value in an instance variable if you refer to the rank more than once per request. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ah, that is great Rob -- thanks!! Ben On 9/27/07, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> > On Sep 27, 2007, at 3:42 PM, Ben Nevile wrote: > > Hi, > > > > Let''s say that in my "users" table I have a column called "points" > > that is indexed. I would like to be able to query the position of any > > given user within that index. In other words, I want the same data > > that the following method would give: > > > > def rank > > result = 0 > > User.find(:all).each {|u| result=result+1 if u.points > points} > > result > > end > > > > However I can''t use the above method because the user database has a > > quarter of a million records and is growing. > > > > Help! and Thanks in advance. > > Ben > > class User > def self.having_more_points_than points > count(:conditions => [ ''points > ?'', points ]) > end > > def rank > self.class.having_more_points_than(self.points) + 1 > end > end > > You might want to save the value in an instance variable if you refer > to the rank more than once per request. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---