<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Yup Dot Com: Polymorphic, Self-Referential ActiveRecord Associations</title>
    <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Advanced Web Services</description>
    <item>
      <title>Polymorphic, Self-Referential ActiveRecord Associations</title>
      <description>&lt;p&gt;&lt;img style="float:right;border: 1px dotted #ccc; padding:4px; margin:4px;" title=" Modernity, Mirrored and Reflected Infinitely" alt="Mirrors" src="http://static.flickr.com/19/22608127_40cb8b4d28_m.jpg" /&gt;Jason King and Josh Susser had an interesting conversation about how to use ActiveRecord to achieve Polymorphic, many-to-many, self-referential associations on the Ruby on Rails mailing list. Jason describes his problem domain as having a:&lt;/p&gt;

&lt;blockquote&gt;
&lt;b&gt;Container&lt;/b&gt; which can contain one or more &lt;b&gt;Element&lt;/b&gt;.  An &lt;b&gt;Element&lt;/b&gt; is a polymorph of either a &lt;b&gt;Container&lt;/b&gt; or a &lt;b&gt;Chunk&lt;/b&gt;.  An Element can exist in one or more &lt;b&gt;Containers&lt;/b&gt;.  I use a table called ownerships as the join between &lt;b&gt;Containers&lt;/b&gt; and &lt;b&gt;Element&lt;/b&gt;.
&lt;/blockquote&gt;

&lt;p&gt;Josh Susser, author of the &lt;a href="http://blog.hasmanythrough.com/"&gt;has_many :through&lt;/a&gt; blog, attempts a solution. He describes his trick, &amp;#8220;You have to disambiguate the two different associations to containers&amp;mdash;one way is as your container, and the other is as an element. That means you need different names for those relationship. I chose &amp;#8220;owner&amp;#8221; to indicate an element&amp;#8217;s container from the element&amp;#8217;s perspective. You also have to use a different name for the two kinds of ownerships, containing and contained.&amp;#8221;&lt;/p&gt;

&lt;p&gt;Read on for the final coded solution.&lt;/p&gt;</description>
      <pubDate>Sat, 20 May 2006 19:39:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:a48bb5be-3273-4cc7-948b-d2508bf03d34</guid>
      <author>Daniel Butler</author>
      <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations</link>
      <category>Ruby on Rails</category>
      <category>Programming</category>
      <category>polymorphic</category>
    </item>
    <item>
      <title>"Polymorphic, Self-Referential ActiveRecord Associations" by barbz</title>
      <description>&lt;p&gt;Nice post!
However, I&amp;#8217;d like chunks to be polymorfic themselves, (e.g. a chunk can be an image, a sound, a video) . Is it feasible? How?&lt;/p&gt;</description>
      <pubDate>Mon, 23 Oct 2006 19:29:26 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:14c31af1-945c-4c64-956e-135a622e813d</guid>
      <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations#comment-275</link>
    </item>
    <item>
      <title>"Polymorphic, Self-Referential ActiveRecord Associations" by Daniel</title>
      <description>&lt;p&gt;Fixed and resacked.&lt;/p&gt;</description>
      <pubDate>Tue, 01 Aug 2006 09:32:53 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:86d5ec51-9dd2-48a7-9915-d4fa307fc3a0</guid>
      <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations#comment-228</link>
    </item>
    <item>
      <title>"Polymorphic, Self-Referential ActiveRecord Associations" by pn</title>
      <description>&lt;p&gt;you have omited to change somme other john , look at the paragraphe juste before the code :)&lt;/p&gt;</description>
      <pubDate>Mon, 31 Jul 2006 05:07:10 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:16034aef-b986-4ee4-ba45-2cd1e0d3b64f</guid>
      <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations#comment-226</link>
    </item>
    <item>
      <title>"Polymorphic, Self-Referential ActiveRecord Associations" by Daniel</title>
      <description>&lt;p&gt;Sorry, Josh. Those responsible have been sacked.&lt;/p&gt;</description>
      <pubDate>Mon, 22 May 2006 06:43:26 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:5d64f9c9-8e4e-479c-89cb-763bcd5e8888</guid>
      <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations#comment-64</link>
    </item>
    <item>
      <title>"Polymorphic, Self-Referential ActiveRecord Associations" by josh susser</title>
      <description>&lt;p&gt;Why do so many people call me John? Other than getting my name wrong, nice article :-)&lt;/p&gt;</description>
      <pubDate>Sun, 21 May 2006 22:42:08 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:245c27aa-bfe4-4334-af14-9d67865923b0</guid>
      <link>http://www.yup.com/articles/2006/05/20/polymorphic-self-referential-activerecord-associations#comment-63</link>
    </item>
  </channel>
</rss>
