<?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>blog.layer2.org &#187; rails</title>
	<atom:link href="http://blog.layer2.org/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.layer2.org</link>
	<description>A technical blog</description>
	<lastBuildDate>Sun, 23 May 2010 00:15:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails migration with a list of all United States</title>
		<link>http://blog.layer2.org/2008/01/12/rails-migration-with-a-list-of-all-united-states/</link>
		<comments>http://blog.layer2.org/2008/01/12/rails-migration-with-a-list-of-all-united-states/#comments</comments>
		<pubDate>Sun, 13 Jan 2008 01:58:09 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2008/01/12/rails-migration-with-a-list-of-all-united-states/</guid>
		<description><![CDATA[How many times have you created web applications that use addresses? Since I felt like I wasted the entire 10 minutes it took me to type out this state list, I figured I&#8217;d share it with everyone so that their time wasn&#8217;t equally wasted. Enjoy.
Run this command:
ruby script/generate model State
And put this in db/migrate/XXX_create_states.rb:

class CreateStates [...]]]></description>
			<content:encoded><![CDATA[<p>How many times have you created web applications that use addresses? Since I felt like I wasted the entire 10 minutes it took me to type out this state list, I figured I&#8217;d share it with everyone so that their time wasn&#8217;t equally wasted. Enjoy.</p>
<p>Run this command:</p>
<pre>ruby script/generate model State</pre>
<p>And put this in db/migrate/XXX_create_states.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> CreateStates &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:states</span> <span style="color:#9966CC; font-weight:bold;">do</span> |t|
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:abbreviation</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Alabama'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'AL'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Alaska'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'AK'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Arizona'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'AZ'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Arkansas'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'AR'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'California'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'CA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Colorado'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'CO'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Connecticut'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'CT'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Delaware'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'DE'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'District of Columbia'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'DC'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Florida'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'FL'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Georgia'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'GA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Hawaii'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'HI'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Idaho'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'ID'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Illinois'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'IL'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Indiana'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'IN'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Iowa'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'IA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Kansas'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'KS'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Kentucky'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'KY'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Louisiana'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'LA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Maine'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'ME'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Maryland'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MD'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Massachutsetts'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Michigan'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MI'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Minnesota'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MN'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Mississippi'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MS'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Missouri'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MO'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Montana'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'MT'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Nebraska'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NE'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Nevada'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NV'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'New Hampshire'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NH'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'New Jersey'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NJ'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'New Mexico'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NM'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'New York'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NY'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'North Carolina'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'NC'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'North Dakota'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'ND'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Ohio'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'OH'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Oklahoma'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'OK'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Oregon'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'OR'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Pennsylvania'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'PA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Rhode Island'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'RI'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'South Carolina'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'SC'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'South Dakota'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'SD'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Tennessee'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'TN'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Texas'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'TX'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Utah'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'UT'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Vermont'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'VT'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Virginia'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'VA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Washington'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'WA'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'West Virginia'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'WV'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Wisconsin'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'WI'</span>
    State.<span style="color:#9900CC;">create</span> <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'Wyoming'</span>, <span style="color:#ff3333; font-weight:bold;">:abbreviation</span> =&gt; <span style="color:#996600;">'WY'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:states</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2008/01/12/rails-migration-with-a-list-of-all-united-states/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Rails&#8217; attachment_fu, :thumbnail_class and you</title>
		<link>http://blog.layer2.org/2007/11/19/rails-attachment_fu-thumbnail_class-and-you/</link>
		<comments>http://blog.layer2.org/2007/11/19/rails-attachment_fu-thumbnail_class-and-you/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 01:56:23 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2007/11/19/rails-attachment_fu-thumbnail_class-and-you/</guid>
		<description><![CDATA[I was having a bit of trouble with attachment_fu that took a while to figure out, so I thought I&#8217;d post my solution for the next person.
I have a Photo model that I&#8217;m using to store pictures. Since attachment_fu will automatically resize, create thumbnails, and store pictures on the file system, it was an easy [...]]]></description>
			<content:encoded><![CDATA[<p>I was having a bit of trouble with attachment_fu that took a while to figure out, so I thought I&#8217;d post my solution for the next person.</p>
<p>I have a Photo model that I&#8217;m using to store pictures. Since attachment_fu will automatically resize, create thumbnails, and store pictures on the file system, it was an easy choice to use it. The things I found I didn&#8217;t like about it:</p>
<ol>
<li>It stores thumbnails as separate records inside the photos table. This means you have to check if thumbnail.nil? each time you display the pictures, and you&#8217;ll have to check parent_id.nil? to count your photos.</li>
<li>You can&#8217;t use your own model validations. For example, I wanted to use</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="ruby">validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>
validates_presence_of <span style="color:#ff3333; font-weight:bold;">:description</span></pre></div></div>

<p>But I&#8217;d always get validation errors because attachment_fu tries to save the thumbnail attributes when it creates or resizes a thumbnail. As you can imagine, this is a major problem.</p>
<h2>:thumbnail_class to the rescue</h2>
<p>Mike Clark&#8217;s <a href="http://clarkware.com/cgi/blosxom/2007/02/24">attachment_fu blog post</a> mentions that you can use the :thumbnail_class argument to separate your model validations and attachment_fu&#8217;s validations. Here&#8217;s how to do it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Photo &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:thumbnails</span>, <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> =&gt; <span style="color:#996600;">'parent_id'</span>
&nbsp;
  has_attachment  <span style="color:#ff3333; font-weight:bold;">:storage</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:file_system</span>,
                          <span style="color:#ff3333; font-weight:bold;">:content_type</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:image</span>,
                          <span style="color:#ff3333; font-weight:bold;">:max_size</span> =&gt; <span style="color:#006666;">10</span>.<span style="color:#9900CC;">megabytes</span>,
                          <span style="color:#ff3333; font-weight:bold;">:resize_to</span> =&gt; <span style="color:#996600;">'640x480'</span>,
                          <span style="color:#ff3333; font-weight:bold;">:thumbnails</span> =&gt; <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:thumb</span> =&gt; <span style="color:#996600;">'100x100'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
                          <span style="color:#ff3333; font-weight:bold;">:thumbnail_class</span> =&gt; Thumbnail
&nbsp;
  <span style="color:#008000; font-style:italic;"># Validations</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:description</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Thumbnail &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:photo</span>, <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> =&gt; <span style="color:#996600;">'parent_id'</span>
&nbsp;
  has_attachment  <span style="color:#ff3333; font-weight:bold;">:storage</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:file_system</span>,
                  <span style="color:#ff3333; font-weight:bold;">:content_type</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:image</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The killer for me initially was that I wasn&#8217;t specifying has_attachment in the Thumbnail model. I always got this error:</p>
<pre>
undefined method `temp_path=' for #thumbnail:0xb69a9514</pre>
<p>So save yourself by putting has_attachment in both models. Make sure to define any attachment_fu arguments in your Photo model, and leave the Thumbnail model bare. You&#8217;ll also want to make sure you have the normal attachment_fu schema in <strong>both</strong> models:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:parent_id</span>,  <span style="color:#ff3333; font-weight:bold;">:integer</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:content_type</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:filename</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:thumbnail</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:size</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:width</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:height</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span></pre></div></div>

<p>I&#8217;ve found that everything seems to be working as normal going this route. My model validations work and the thumbnails are not polluting the photos table. If you want to find the Photo for a particular Thumbnail, keep in mind that parent_id now refers to the id in the Photo model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">t = Thumbnail.<span style="color:#9900CC;">find</span> ....  <span style="color:#008000; font-style:italic;"># find your thumbnail</span>
<span style="color:#CC0066; font-weight:bold;">p</span> = Photo.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>t.<span style="color:#9900CC;">parent_id</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>And for the ultimate ease-of-use relationship, just use:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">t = Thumbnail.<span style="color:#9900CC;">find</span> ....  <span style="color:#008000; font-style:italic;"># find your thumbnail</span>
<span style="color:#CC0066; font-weight:bold;">p</span> = t.<span style="color:#9900CC;">photo</span>               <span style="color:#008000; font-style:italic;"># get a photo</span>
<span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">thumbnails</span>              <span style="color:#008000; font-style:italic;"># get all thumbnails</span></pre></div></div>

<p>This works since we defined the has_many relationship in the model.</p>
<p>Let me know if you have any problems with this method or if it helped you!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2007/11/19/rails-attachment_fu-thumbnail_class-and-you/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How to create a lookup table in Ruby on Rails</title>
		<link>http://blog.layer2.org/2007/10/18/how-to-create-a-lookup-table-in-ruby-on-rails/</link>
		<comments>http://blog.layer2.org/2007/10/18/how-to-create-a-lookup-table-in-ruby-on-rails/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 01:02:53 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2007/10/18/how-to-create-a-lookup-table-in-ruby-on-rails/</guid>
		<description><![CDATA[I&#8217;m a big fan of the Rails way, but sometimes the simple things get you. I like to set up &#8220;lookup tables&#8221; in Rails, ie. tables in the database that hold commonly used, (often) fixed values. For example, if I have a Car model, I might want to define a Car type model. This would [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of <em>the Rails way</em>, but sometimes the simple things get you. I like to set up &#8220;lookup tables&#8221; in Rails, ie. tables in the database that hold commonly used, (often) fixed values. For example, if I have a Car model, I might want to define a Car type model. This would have two purposes:</p>
<ol>
<li>Give me the ability to associate a type with the Car model (ie. Car.type => &#8217;sport&#8217;)</li>
<li>Pre-load common values into the database (ie. sport, coupe, sedan, etc)</li>
</ol>
<p><span id="more-55"></span></p>
<p>I can accomplish this through a few steps. First, lets generate the lookup table model:</p>
<pre>
$ ruby script/generate model CarType</pre>
<p>We&#8217;ll need to edit the migration under db/migrate/XXX_create_car_types.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> CreateCarTypes &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:car_types</span> <span style="color:#9966CC; font-weight:bold;">do</span> |t|
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    CarType.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'sedan'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    CarType.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'sport'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    CarType.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'coupe'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    CarType.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'truck'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    CarType.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'van'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:car_types</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You&#8217;ll note that I created the <em>name</em> field, and then I just used the create method to produce sample values in the database. This can be an immensely useful technique, especially when deploying a production website &#8211; just run your migrations and those lookup tables are already populated.</p>
<p><strong>Note: </strong>The other technique for populating the database automatically is fixtures. While I think they are great for creating test development data, fixtures fall behind in a production environment. Typically, fixtures will define data like &#8220;Test car 1&#8243; and &#8220;Test car 2&#8243;. This is very useful to during development, but if you have a lot of test data you can crowd your production database pretty quickly. Since there is no mechanism to conditionally load fixture data based on the environment, fixtures lose their appeal. If you must load fixture data into your production database, you may use</p>
<pre>
$ rake RAILS_ENV=production db:fixtures:load</pre>
<p>Second, create the Car model and associate it with a CarType:</p>
<pre>
$ ruby script/generate model Car</pre>

<div class="wp_syntax"><div class="code"><pre class="ruby">Car &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:car_type</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Your Car migration must hold the appropriate car_type_id:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> CreateCars &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:cars</span> <span style="color:#9966CC; font-weight:bold;">do</span> |t|
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:car_type_id</span>, <span style="color:#ff3333; font-weight:bold;">:int</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:cars</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then, you can write code like this:</p>
<pre>
c = Car.find(1)
c.car_type
=> #<CarType:0xb70f05c0 @attributes={"name"=>"sedan", "id"=>"1"}>
</pre>
<p>I like to simply even more, so I generally add a method like this to the Car model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> type
  car_type.<span style="color:#9900CC;">name</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Resulting in the following:</p>
<pre>
c.type
=> "sedan"</pre>
<p>And thats it!</p>
<p>You might be interested to know that you <em>can</em> add a corresponding has_many to the CarType model to easily find all Cars of a particular type:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">CarType &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:cars</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And then call something like:</p>
<pre>
CarType.find(1).cars
=> [#<Car:0xb70f4c88 @attributes={"name"=>"Test car 1", "id"=>"1", "car_type_id"=>"1"}>]
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2007/10/18/how-to-create-a-lookup-table-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New version of Rails available</title>
		<link>http://blog.layer2.org/2007/10/18/new-version-of-rails-available/</link>
		<comments>http://blog.layer2.org/2007/10/18/new-version-of-rails-available/#comments</comments>
		<pubDate>Thu, 18 Oct 2007 23:49:26 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2007/10/18/new-version-of-rails-available/</guid>
		<description><![CDATA[If you didn&#8217;t know already, there is a new version of Rails out there.
And I&#8217;m not referring to the Rails 2.0 preview. This is Rails 1.2.5, which contains fixes for a JSON XSS (cross-site scripting) vulnerability. I&#8217;m not horribly familiar with the details, but the site does say that you don&#8217;t have to worry about [...]]]></description>
			<content:encoded><![CDATA[<p>If you didn&#8217;t know already, there is a new version of <a href="http://weblog.rubyonrails.org/2007/10/12/rails-1-2-5-maintenance-release">Rails out there</a>.</p>
<p>And I&#8217;m not referring to the <a href="http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release">Rails 2.0 preview</a>. This is Rails 1.2.5, which contains fixes for a JSON XSS (cross-site scripting) vulnerability. I&#8217;m not horribly familiar with the details, but the site does say that you don&#8217;t have to worry about it if you&#8217;re not using JSON. Probably a good idea to <a href="http://www.rubyonrails.org/down">upgrade</a> anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2007/10/18/new-version-of-rails-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails&#8217; ruby script/console has tab completion!</title>
		<link>http://blog.layer2.org/2007/10/12/rails-ruby-scriptconsole-has-tab-completion/</link>
		<comments>http://blog.layer2.org/2007/10/12/rails-ruby-scriptconsole-has-tab-completion/#comments</comments>
		<pubDate>Fri, 12 Oct 2007 22:48:53 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2007/10/12/rails-ruby-scriptconsole-has-tab-completion/</guid>
		<description><![CDATA[While I recently found out about Ruby&#8217;s &#8216;methods&#8217; method, I also found that Rails&#8217; &#8220;ruby script/console&#8221; will do tab completion:

&#62;&#62; a = Array.new
=&#62; []
&#62;&#62; a.
Display all 157 possibilities? (y or n)
a.fetch                         [...]]]></description>
			<content:encoded><![CDATA[<p>While I recently found out about Ruby&#8217;s &#8216;methods&#8217; method, I also found that Rails&#8217; &#8220;ruby script/console&#8221; will do tab completion:</p>
<pre>
&gt;&gt; a = Array.new
=&gt; []
&gt;&gt; a.
Display all 157 possibilities? (y or n)
a.fetch                              a.method                             a.slice
a.__id__                             a.fill                               a.methods                            a.slice!
a.__send__                           a.find                               a.min                                a.sort
a.all?                               a.find_all                           a.nil?                               a.sort!
a.any?                               a.first                              a.nitems                             a.sort_by
a.assoc                              a.flatten                            a.object_id                          a.split
a.at                                 a.flatten!                           a.pack                               a.subclasses_of
a.b64encode                          a.freeze                             a.partition                          a.sum
a.blank?                             a.frozen?                            a.pop                                a.suppress
a.class                              a.gem                                a.pretty_inspect                     a.taguri
a.clear                              a.grep                               a.pretty_print                       a.taguri=
a.clone                              a.group_by                           a.pretty_print_cycle                 a.taint
a.collect                            a.hash                               a.pretty_print_inspect               a.tainted?
a.collect!                           a.id                                 a.pretty_print_instance_variables    a.to_a
a.compact                            a.in_groups_of                       a.private_methods                    a.to_ary
a.compact!                           a.include?                           a.protected_methods                  a.to_default_s
a.concat                             a.index                              a.public_methods                     a.to_formatted_s
a.copy_instance_variables_from       a.index_by                           a.push                               a.to_json
a.daemonize                          a.indexes                            a.rassoc                             a.to_param
a.dclone                             a.indices                            a.reject                             a.to_s
a.decode64                           a.inject                             a.reject!                            a.to_sentence
a.decode_b                           a.insert                             a.remove_subclasses_of               a.to_set
a.delete                             a.inspect                            a.replace                            a.to_xml
a.delete_at                          a.instance_eval                      a.require                            a.to_yaml
a.delete_if                          a.instance_exec                      a.require_gem                        a.to_yaml_properties
a.detect                             a.instance_of?                       a.require_library_or_gem             a.to_yaml_style
a.display                            a.instance_values                    a.respond_to?                        a.transpose
a.dup                                a.instance_variable_get              a.returning                          a.type
a.each                               a.instance_variable_set              a.reverse                            a.uniq
a.each_index                         a.instance_variables                 a.reverse!                           a.uniq!
a.each_with_index                    a.is_a?                              a.reverse_each                       a.unloadable
a.empty?                             a.join                               a.rindex                             a.unshift
a.enable_warnings                    a.kind_of?                           a.select                             a.untaint
a.encode64                           a.last                               a.send                               a.values_at
a.entries                            a.length                             a.shift                              a.with_options
a.eql?                               a.load                               a.silence_stderr                     a.yaml_initialize
a.equal?                             a.map                                a.silence_stream                     a.zip
a.extend                             a.map!                               a.silence_warnings
a.extend_with_included_modules_from  a.max                                a.singleton_methods
a.extended_by                        a.member?                            a.size</pre>
<p>Unfortunately the same thing doesn&#8217;t work for the irb. Of course you can always</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">a.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">sort</span></pre></div></div>

<p>to see the same information.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2007/10/12/rails-ruby-scriptconsole-has-tab-completion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert has_and_belongs_to_many to a has_many :through association</title>
		<link>http://blog.layer2.org/2007/10/10/convert-has_and_belongs_to_many-to-a-has_many-through-association/</link>
		<comments>http://blog.layer2.org/2007/10/10/convert-has_and_belongs_to_many-to-a-has_many-through-association/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 03:23:49 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2007/10/10/convert-has_and_belongs_to_many-to-a-has_many-through-association/</guid>
		<description><![CDATA[So there are plenty of resources out there to learn how to use has_many :through associations.
I followed them over and over again but couldn&#8217;t get my code to work. I knew I had the basic structure setup correctly, since the examples are pretty straightforward, and the concept is not difficult. My has_and_belongs_to_many code originally looked [...]]]></description>
			<content:encoded><![CDATA[<p>So there are plenty of resources out there to <a href="http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off">learn</a> <a href="http://matthewman.net/2006/01/06/rails-activerecord-goes-through/">how</a> to use has_many :through associations.</p>
<p>I followed them over and over again but couldn&#8217;t get my code to work. I knew I had the basic structure setup correctly, since the examples are pretty straightforward, and the concept is not difficult. My has_and_belongs_to_many code originally looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Soda &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:distributors</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Distributor &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:sodas</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Of course there was also a many-to-many join table migration:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> DistributorsSodasJoinTable &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:distributors_sodas</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> =&gt; <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">do</span> |t|
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:soda_id</span>, <span style="color:#ff3333; font-weight:bold;">:int</span>
      t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:distributor_id</span>, <span style="color:#ff3333; font-weight:bold;">:int</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>  
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:distributors_sodas</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This works quite nicely:</p>
<pre>
>> Soda.find(1).distributors
=> []</pre>
<p>Later found that I needed to add attributes in the join table to associate extra fields on the Distributors <-> Sodas relationship. has_and_belongs_to_many does not have a Rails way to access those extra fields in the join table. I&#8217;ve successfully done it through SQL, but much guilt and remorse lead me to finally learn has_many :through.</p>
<p>This was my best initial attempt:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Soda &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:distributors_sodas</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:distributors</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> =&gt;; <span style="color:#ff3333; font-weight:bold;">:distributors_sodas</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Distributor &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:distributors_sodas</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:distributors</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:distributors_sodas</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> DistributorsSodas &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:soda</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:distributor</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>All goes well until I try to do a quick test:</p>
<pre>
>> Soda.find(1).distributors
NameError: uninitialized constant Soda::DistributorsSoda
...
        from (irb):4</pre>
<p>Umm&#8230; what? I never tried to instantiate an object of the type Soda::DistributorsSoda. Instead, I was simply trying to use the DistributorsSodas ActiveRecord object, right?</p>
<p>It turns out that has_many :through (apparently) can&#8217;t handle using the join tables created by has_and_belongs_to_many. Its just a naming issue &#8211; has_many :through will work fine using a one-to-many join table like distributor_sodas (note the missing &#8217;s&#8217; on distributor). If you need a many-to-many join, you have to rename the table to fix the (pluralization?) problem. I deleted the DistributorsSodas model and created the Store model.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Soda &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:stores</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:distributors</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:stores</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Distributor &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:stores</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:sodas</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:stores</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Store &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:soda</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:distributor</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This proved a much better result:</p>
<pre>
>> Soda.find(1).distributors
=> []</pre>
<p>In the end, the association naming convention actually make more sense. I was bummed to have to change the table/model names though.</p>
<p>Please comment if you know how to create the association without changing the model name.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2007/10/10/convert-has_and_belongs_to_many-to-a-has_many-through-association/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Installing Mongrel on Ubuntu Feisty with rubygems</title>
		<link>http://blog.layer2.org/2007/10/03/installing-mongrel-on-ubuntu-feisty-with-rubygems/</link>
		<comments>http://blog.layer2.org/2007/10/03/installing-mongrel-on-ubuntu-feisty-with-rubygems/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 04:00:04 +0000</pubDate>
		<dc:creator>solipsistic</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.layer2.org/2007/10/03/installing-mongrel-on-ubuntu-feisty-with-rubygems/</guid>
		<description><![CDATA[When I recently re-installed Ubuntu 7.04 (Feisty), I found that I couldn&#8217;t build the native fastthread gem (a required dependency). If you get this error:

Building native extensions.  This could take a while...
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:1

ERROR:  While executing gem ... (RuntimeError)
ERROR: Failed to build gem native extension.
Gem [...]]]></description>
			<content:encoded><![CDATA[<p>When I recently re-installed Ubuntu 7.04 (Feisty), I found that I couldn&#8217;t build the native fastthread gem (a required dependency). If you get this error:<br />
<code><br />
Building native extensions.  This could take a while...<br />
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)<br />
from extconf.rb:1</code><br />
<code><br />
ERROR:  While executing gem ... (RuntimeError)<br />
ERROR: Failed to build gem native extension.<br />
Gem files will remain installed in /var/lib/gems/1.8/gems/fastthread-1.0 for inspection.<br />
</code><br />
then you need the ruby development libraries and the build-essential package. I&#8217;ve found that these commands will make sure mongrel (and rails) is installed correctly:<br />
<code><br />
$ sudo apt-get install ruby1.8-dev rubygems build-essential rails<br />
$ sudo gem install mongrel --include-dependencies<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.layer2.org/2007/10/03/installing-mongrel-on-ubuntu-feisty-with-rubygems/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.886 seconds -->
