<?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: Tag polymorphic</title>
    <link>http://www.yup.com/articles/tag/polymorphic?tag=polymorphic</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>
  </channel>
</rss>
