<?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>Pivotal Labs &#187; Yaneeka Huq</title>
	<atom:link href="http://pivotallabs.com/author/yhuq/feed/" rel="self" type="application/rss+xml" />
	<link>http://pivotallabs.com</link>
	<description>Agility Developed</description>
	<lastBuildDate>Wed, 19 Jun 2013 16:27:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Standup 7/16/2012: STI + Polymorphism needs base class</title>
		<link>http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/</link>
		<comments>http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/#comments</comments>
		<pubDate>Mon, 16 Jul 2012 14:49:00 +0000</pubDate>
		<dc:creator>Yaneeka Huq</dc:creator>
				<category><![CDATA[Standup]]></category>
		<category><![CDATA[agile]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/</guid>
		<description><![CDATA[<p><h2>Interesting Things</h2>

<ul>
<li>STI + Polymorphism needs base class</li>
</ul>

<pre>
class Movie &#60; ActiveRecord::Base
  has_one :player, :as =&#62; :content
end

class Player &#60; ActiveRecord::Base
  belongs_to :content, :polymorphic =&#62; true
end
</pre>

<p>and then, later we refactor Movie to use STI with Product... </p>

<pre>
class Product &#60; ActiveRecord::Base ... end
class Movie &#60; Product ... end

&#62; movie = Movie.first
&#62; movie.player
  Player Load &#40;1.1ms&#41;  SELECT &#34;players&#34;.* FROM &#34;players&#34; WHERE &#34;players&#34;.&#34;content_id&#34; = 144 AND &#34;players&#34;.&#34;content_type&#34; = 'Product' LIMIT 1
 =&#62; nil
</pre>

<p>because players.content_type == 'Movie' but polymorphism uses the base class, 'Product'</p>

<p>Fix: Add a migration to change the content_type column</p>

<p>UPDATE players SET content_type='Product' WHERE content_type='Movie'</p> <a href="http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>The post <a href="http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/">Standup 7/16/2012: STI + Polymorphism needs base class</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<h2>Interesting Things</h2>
<ul>
<li>STI + Polymorphism needs base class</li>
</ul>
<pre>
class Movie &lt; ActiveRecord::Base
  has_one :player, :as =&gt; :content
end

class Player &lt; ActiveRecord::Base
  belongs_to :content, :polymorphic =&gt; true
end
</pre>
<p>and then, later we refactor Movie to use STI with Product&#8230; </p>
<pre>
class Product &lt; ActiveRecord::Base ... end
class Movie &lt; Product ... end

&gt; movie = Movie.first
&gt; movie.player
  Player Load &#40;1.1ms&#41;  SELECT &quot;players&quot;.* FROM &quot;players&quot; WHERE &quot;players&quot;.&quot;content_id&quot; = 144 AND &quot;players&quot;.&quot;content_type&quot; = 'Product' LIMIT 1
 =&gt; nil
</pre>
<p>because players.content_type == &#8216;Movie&#8217; but polymorphism uses the base class, &#8216;Product&#8217;</p>
<p>Fix: Add a migration to change the content_type column</p>
<p>UPDATE players SET content_type=&#8217;Product&#8217; WHERE content_type=&#8217;Movie&#8217;</p>
<p>The post <a href="http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/">Standup 7/16/2012: STI + Polymorphism needs base class</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/standup-7-16-2012-sti-polymorphism-needs-base-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using apc
Database Caching using apc
Object Caching 393/411 objects using apc

 Served from: pivotallabs.com @ 2013-06-19 13:37:03 by W3 Total Cache -->