<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wil Gieseler &#187; Code</title>
	<atom:link href="http://wil.gieseler.org/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://wil.gieseler.org</link>
	<description>is delicious.</description>
	<lastBuildDate>Tue, 24 Aug 2010 22:33:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Rankitize</title>
		<link>http://wil.gieseler.org/2009/12/rankitize/</link>
		<comments>http://wil.gieseler.org/2009/12/rankitize/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 23:16:41 +0000</pubDate>
		<dc:creator>Wil Gieseler</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://wil.gieseler.org/?p=202</guid>
		<description><![CDATA[A plugin to get the rank of models from a MySQL database (which lacks a ranking function) without loading them all.]]></description>
			<content:encoded><![CDATA[<p>Have you ever been working on a <a href="http://coolpoints.net">big awesome website</a> using Ruby on Rails and MySQL and had the need to rank your models by an attribute without loading them all into memory? I know I have.</p>
<p>Unfortunately, unlike Postgres and some other SQL flavors, MySQL lacks a built-in ranking function. After some cursory googling, I found <a href="http://arjen-lentz.livejournal.com/56292.html">Arjen Lentz&#8217;s solution</a> which <a href="http://www.chadwickwood.com/2009/07/easy-ranking-with-rails-and-mysql">Chadwick Wood put into use in Rails</a>. However, it&#8217;s a little clunky to put all that code in your model for every category you might want to rank, so I DRY&#8217;d it up and put together <a href="http://github.com/supapuerco/rankitize">a quick little ActiveRecord plugin</a> to do just that.</p>
<p>It&#8217;s easy to use, in your model just declare the columns you want to be able to fetch ranks for:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
    rank_by <span style="color:#ff3333; font-weight:bold;">:score</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>&#8230; and with a little dynamic business you can call a method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">score_ranking</span></pre></div></div>

<p>So there you go.</p>
<p>I&#8217;m willing to bet that this won&#8217;t perform well on extremely large datasets, but I haven&#8217;t found a better solution for MySQL. For the time being, you probably will want to cache this value somewhere.</p>
<p>You can find out how to install the plugin by visiting <a href="http://github.com/supapuerco/rankitize">Rankitize on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://wil.gieseler.org/2009/12/rankitize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
