<?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>a work on process &#187; has_many :through</title>
	<atom:link href="http://jystewart.net/process/tag/has_many-through/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Sat, 21 Aug 2010 13:09:59 +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>Rails&#8217; Improved Join Model Creation</title>
		<link>http://jystewart.net/process/2006/08/rails-improved-join-model-creation/</link>
		<comments>http://jystewart.net/process/2006/08/rails-improved-join-model-creation/#comments</comments>
		<pubDate>Sun, 20 Aug 2006 01:26:58 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[associations]]></category>
		<category><![CDATA[edge rails]]></category>
		<category><![CDATA[has_many :through]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/archives/2006/08/rails-improved-join-model-creation/</guid>
		<description><![CDATA[Jeremy Kemper has just checked in a change to Edge Rails that fixes my biggest frustration with has_many :through. Since it&#8217;s appearance a couple of versions back, has_many :through has been a great addition to the rails associations toolkit, making the use of join models infinitely cleaner. But until recently it was rather cumbersome to <a href="http://jystewart.net/process/2006/08/rails-improved-join-model-creation/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>Jeremy Kemper has just checked in a change to Edge Rails that fixes my biggest frustration with <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530"><em>has_many :through</em></a>. Since it&#8217;s appearance a couple of versions back, <em>has_many :through</em> has been a great addition to the rails associations toolkit, making the use of join models infinitely cleaner. But until recently it was rather cumbersome to create those relationships.</p>
<p>For example, if I wanted to add an author to a book where the relationship is defined as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">has_many <span style="color:#ff3333; font-weight:bold;">:authors</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:involvements</span>, 
  <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Contributor'</span></pre></div></div>

<p>The simplest it could be to find and create an author and set up a link was:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">book.<span style="color:#9900CC;">involvements</span>.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>
  <span style="color:#ff3333; font-weight:bold;">:author</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Contributor.<span style="color:#9900CC;">find_or_create_by_name</span><span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
book.<span style="color:#9900CC;">authors</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now it&#8217;s as simple as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">book.<span style="color:#9900CC;">authors</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span> Contributor.<span style="color:#9900CC;">find_or_create_by_name</span><span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The one gotcha comes if your book object is a new record, and so doesn&#8217;t yet have an ID. Trying the above in that case will raise ActiveRecord::HasManyThroughCantAssociateNewRecords.</p>
<p>Since in my particular case I&#8217;m masking the association behind a proxy attribute, I considered getting around the problem by dynamically creating (and subsequently removing) <a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#M000784">after_save callbacks</a>, but for now I&#8217;ve simply changed my controller to create the associations after the record is saved. Maybe Edge Rails will have some more options to offer us before long.</p>
<p>Thanks to Josh Susser for the original report. Check out <a href="http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation">his post</a> for more detail and examples.</pre>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2006/08/rails-improved-join-model-creation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
