<?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>Nomadicoder &#187; HTML</title>
	<atom:link href="http://nomadicoder.com/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://nomadicoder.com</link>
	<description>Coding wherever I happen to be</description>
	<lastBuildDate>Tue, 01 Mar 2011 16:43:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Ordering on a Field in a Linked Table</title>
		<link>http://nomadicoder.com/2010/02/01/ordering-on-a-field-in-a-linked-table/</link>
		<comments>http://nomadicoder.com/2010/02/01/ordering-on-a-field-in-a-linked-table/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 05:55:19 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://nomadicoder.com/2010/02/01/ordering-on-a-field-in-a-linked-table/</guid>
		<description><![CDATA[I attempted to order my runners based on their assigned stage number. The stage number is linked by the stage_id in the schema and the class shows that it belongs to a stage: class Runner &#62; ActiveRecord::Base belongs_to :stage end &#8230; <a href="http://nomadicoder.com/2010/02/01/ordering-on-a-field-in-a-linked-table/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I attempted to order my runners based on their assigned stage number. The stage number is linked by the stage_id in the schema and the class shows that it belongs to a stage:</p>
<pre>
<code>
class Runner &gt; ActiveRecord::Base
  belongs_to :stage
end
</code>
</pre>
<p>At first glance, I figure I would simply order on the stage number:</p>
<pre>
<code>
def self.find_all_runners
  find ( :all, :order =&gt; "stage.number" )
end
</code>
</pre>
<p>But this resulted in a &#8220;no such column&#8221; error:</p>
<p><em><span style="color: #ff1f19;">SQLite3::SQLException: no such column: stages.number: SELECT * FROM &#8220;runners&#8221; ORDER BY stages.number</span></em></p>
<p>I found the solution in Ryan Daigle&#8217;s <a href="http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions">blog</a>. In order for this to work you need a <em>:joins</em> to the linked table so Active Record knows the origin of stage.number. In my application, the find should be:</p>
<pre>
<code>
def self.find_all_runners
  find ( :all, :joins =&gt; :stage, :order =&gt; "stages.number" )
end
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://nomadicoder.com/2010/02/01/ordering-on-a-field-in-a-linked-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

