<?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 Twofer &#187; STI</title>
	<atom:link href="http://www.atwofer.com/tag/sti/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.atwofer.com</link>
	<description>A place to talk about Software Development (RoR, J2EE, J2ME, CSS, HTML, JavaScript, Objective-C, etc) and some other stuff</description>
	<lastBuildDate>Fri, 16 Jul 2010 14:56:02 +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>Single Table Inheritance and accepts_nested_attributes_for</title>
		<link>http://www.atwofer.com/2009/11/02/single-table-inheritance-and-accepts_nested_attributes_for/</link>
		<comments>http://www.atwofer.com/2009/11/02/single-table-inheritance-and-accepts_nested_attributes_for/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 00:42:36 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[accepts_nested_attributes_for]]></category>
		<category><![CDATA[STI]]></category>

		<guid isPermaLink="false">http://www.atwofer.com/?p=129</guid>
		<description><![CDATA[I recently upgraded our web app at Episodic from Rails 2.0 to Rails 2.3.4.  While this resulted in many issues we had to resolve that I will try to write about in later post, it does allow me to take advantage of some of the Rails 2.3.x features like accepts_nested_attributes_for.  If you aren’t [...]]]></description>
			<content:encoded><![CDATA[<p>I recently upgraded our web app at <a href="http://www.episodic.com">Episodic</a> from Rails 2.0 to Rails 2.3.4.  While this resulted in many issues we had to resolve that I will try to write about in later post, it does allow me to take advantage of some of the Rails 2.3.x features like accepts_nested_attributes_for.  If you aren’t familiar with accepts_nested_attributes_for then check out <a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes">Ryan’s post</a>.</p>
<p>So I went ahead and tried to make use of this in my object model.  Here are the important parts of the model.</p>
<p><code><br />
<span>class Episode < ActiveRecord::Base</span><br />
<span style="padding-left: 20px">has_many :field_values, :dependent => :destroy</span><br />
<span style="padding-left: 20px">accepts_nested_attributes_for :field_values, :allow_destroy => true</span><br />
<span>end</span><br />
<span></span><br />
<span>class FieldValue < ActiveRecord::Base</span><br />
<span style="padding-left: 20px">:belongs_to :episode</span><br />
<span>end</span><br />
<span></span><br />
<span>class TextFieldValue < FieldValue</span><br />
<span>end</span><br />
<span></span><br />
<span>class NumberFieldValue < FieldValue</span><br />
<span>end</span><br />
</code></p>
<p>Since I am using Single Table Inheritance I end up with a field_values table that has a column named “type” which contain either a value of “TextFieldValue” or “NumberFieldValue”.</p>
<p>Also, because I enabled accepts_nested_attributes_for on my Episode class I should be able to do something like:</p>
<p><code><br />
<span>episode.field_values_attributes = </span><br />
<span style="padding-left: 20px">[{:value => “foo”, :type => “TextFieldValue”}]</span><br />
</code></p>
<p>However, when I look in the DB I see that there is a new row but the “type” column is NULL even though I set it to “TextFieldValue”.  When I look in the logs I see: &#8220;Can&#8217;t mass-assign these protected attributes: type&#8221;.</p>
<p>Luckily, there is a way around this.  I added a setter called &#8220;value_type&#8221; to my FieldValue class.</p>
<p><code><br />
<span>def value_type= value_type</span><br />
<span style="padding-left: 20px">self.type = value_type</span><br />
<span>end</span><br />
</code></p>
<p>Now, when I can safely use field_values_attributes and set a type.</p>
<p><code><br />
<span>episode.field_values_attributes = </span><br />
<span style="padding-left: 20px">[{:value => “foo”, :value_type => “TextFieldValue”}]</span><br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.atwofer.com/2009/11/02/single-table-inheritance-and-accepts_nested_attributes_for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
