<?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; before_type_cast</title>
	<atom:link href="http://www.atwofer.com/category/before_type_cast/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>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>
