title: Double Polymorphic Association in Rails
body: <p>I have a join table and I would like both the belongs_to associations to be polymorphic. The relationship would look as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/KdqIt.jpg" alt="Double Polymorhpic"></p>
  
  <p>The class structure would look as follows:</p>
  
  <pre><code>Class Tagable1 &lt; ActiveRecord::Base
    has_many :taggings
    has_many :tag_group_1s, :through =&gt; :taggings
    has_many :tag_group_2s, :through =&gt; :taggings
    ...
  end
  
  Class TagGroup1 &lt; ActiveRecord::Base
    has_many :taggings
    has_many :taggable_1s, :through =&gt; :taggings
    has_many :taggable_2s, :through =&gt; :taggings
    ...
  end
  
  Class Tagging &lt; ActiveRecord::Base
    belongs_to :taggable, :polymorphic =&gt; true
    belongs_to :tag, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p>There's obvious drawbacks to this approach, especially the heavy load on the Taggings table. What I'm trying to achieve is a flexible design which allows me to create new Taggables or TagGroups without creating new join tables. For instance, without the double polymorphic, Taggable1 would need a Taggable1Taggings table to join Taggable1 with the polymorphic TagGroups. Likewise, I would need Taggable2Taggings, Taggable3Taggings, Taggable4Taggings, etc etc. </p>
  
  <p>I would love to be enlightened if there are drawbacks I'm not thinking of.</p>