<?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; Ruby on Rails</title>
	<atom:link href="http://www.atwofer.com/category/ruby-on-rails/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>
		<item>
		<title>Override attribute accessor and before_type_cast</title>
		<link>http://www.atwofer.com/2009/02/17/override-attribute-accessor-and-before_type_cast/</link>
		<comments>http://www.atwofer.com/2009/02/17/override-attribute-accessor-and-before_type_cast/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 03:45:24 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[alias_method]]></category>
		<category><![CDATA[before_type_cast]]></category>

		<guid isPermaLink="false">http://www.atwofer.com/?p=78</guid>
		<description><![CDATA[Our Episodic Video Publishing Web Application is written in Ruby on Rails.  I recently came across a situation where I had a model where I wanted to override it&#8217;s attribute accessors in some cases.  Consider a model that looks like this.
create_table "podcasts", :force =&#62; true do &#124;t&#124;
t.string   "name"
t.boolean  "use_show_name", :default [...]]]></description>
			<content:encoded><![CDATA[<p>Our <a href="http://www.episodic.com/" target="_blank">Episodic Video Publishing Web Application</a> is written in Ruby on Rails.  I recently came across a situation where I had a model where I wanted to override it&#8217;s attribute accessors in some cases.  Consider a model that looks like this.</p>
<p><code>create_table "podcasts", :force =&gt; true do |t|<br />
<span style="padding-left: 10px;">t.string   "name"</span><br />
<span style="padding-left: 10px;">t.boolean  "use_show_name", :default =&gt; true</span><br />
<span style="padding-left: 10px;">t.integer   "show_id"</span><br />
end</code></p>
<p>As you can see above that each podcast belongs to a show.  I also have a form for this model and in the name field I want to display the name of the podcast unless &#8220;use_show_name&#8221; is true then I want to display the name of the show that this podcast belongs to.</p>
<p><code>&lt;%= text_field :podcast, :name, :class =&gt; "text", :disabled =&gt; @podcast.use_show_name %&gt;</code></p>
<p>I don&#8217;t want to put this logic in the ERB template but instead I want to put it in the model so that anyone that calls Podcast.name will get the appropriate value depending on the &#8220;use_show_name&#8221;.</p>
<p><code>class Podcast &lt; ActiveRecord::Base<br />
<span style="padding-left: 10px;">belongs_to :show</span><br />
<span>&nbsp;</span><br />
<span style="padding-left: 10px;">def name</span><br />
<span style="padding-left: 20px;">return self.use_show_name ? self.show.name : read_attribute(:name)</span><br />
<span style="padding-left: 10px;">end</span><br />
<span style="padding-left: 10px;">alias_method :name_before_type_cast, :name</span><br />
end</code></p>
<p>So there a few things going on above.</p>
<ul>
<li>We override the &#8220;name&#8221; method by simply implementing it.  This works because ActiveRecord using method_missing to implement it&#8217;s &#8220;name&#8221; getter but since there is now a &#8220;name&#8221; method, method_missing will not be invoked.</li>
<li>In the method we check &#8220;use_show_name&#8221; to see what value to return.  If it is true we get the value from the show object.  Otherwise, we call read_attribute to get the value of the name attribute that stored in the DB.</li>
<li>Lastly, we alias the name method as &#8220;name_before_type_cast&#8221; since this is the method that is called by FormHelper.text_field.  ActiveRecord form helpers call the before_type_cast so that they can get the string value of the attribute but we want it to get the value from our &#8220;name&#8221; method.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.atwofer.com/2009/02/17/override-attribute-accessor-and-before_type_cast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
