Let's say I have Posts
and Comments
for a blog app using Rails and MySQL.
I want to rank Posts by number of comments. (Note: not just sort, but get their actual 1st,2nd,3rd rank).
Also, given a post, I want to be able to get it's rank without loading all Posts into Rails and searching through them.
e.g. "This post is ranked #372 by number of comments"
Finally, if two posts have the same number of comments, they should have the same rank - so ties are ok.
I've seem some clever solutions to this in MySQL, such as this post: How do I Handle Ties When Ranking Results in MySQL?
I'm wondering if there is a simpler solution by normalizing some of the data in an additional field on the posts table.
Has anyone seen a good approach for this?