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
On Sep 27, 2007, at 3:42 PM, Ben Nevile wrote:
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 B. http://agileconsultingllc.com
[email protected]
ah, that is great Rob – thanks!!
Ben