title: Separate Polymorphic Association table
body: <p>After reading the Ruby on Rails guides and a few of the stackoverflow responses to questions about polymorphic association I understand its use and implementation but I have a question about a specific use scenario. I have <code>tags</code> that can be associated with multiple <code>topics</code>, <code>categories</code>, <code>images</code> and other various models (which also have varying <code>tags</code>) but instead of placing the reference fields (<code>foreign_id</code>, <code>foreign_type</code>) within the <code>tags</code> table, I'd prefer to create a separate association table. Is this still possible using <code>:polymorphic =&gt; true</code>?</p>
  
  <p>Something like this:</p>
  
  <pre><code>create_table :tags do |t|
    t.string :name
    t.remove_timestamps
  end
  
  create_table :object_tags, :id =&gt; false do |t|
    t.integer :tag_id
    t.references :tagable, :polymorphic =&gt; true
    t.remove_timestamps
  end
  </code></pre>
  
  <p>If this isn't possible, I was planning on creating the same <code>:object_tags</code> table and using <code>:conditions</code> within the <code>Tag</code> model and other models to force the associations. Is there a rails way of doing this? Thanks! (working with rails 3.0.9 &amp; ruby 1.8.7 &lt;- because deployment server is still using 1.8.7)</p>
  
  <p><strong>UPDATE:</strong>
  Thanks Delba! <a href="https://stackoverflow.com/questions/6964678/habtm-polymorphic-relationship">Answer</a> is a working solution for HABTM polymorphism.</p>
  
  <pre><code>class Tag &lt; ActiveRecord::Base
    has_many :labels
  end
  
  class Label &lt; ActiveRecord::Base
    belongs_to :taggable, :polymorphic =&gt; true
    belongs_to :tag
  end
  
  class Topic &lt; ActiveRecord::Base
    has_many :labels, :as =&gt; :taggable
    has_many :tags, :through =&gt; :labels
  end
  
  create_table :tags, :timestamps =&gt; false do |t|
    t.string :name
  end
  
  create_table :labels, :timestamps =&gt; false, :id =&gt; false do |t|
    t.integer :tag_id
    t.references :taggable, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p><strong>UPDATE:</strong> Because I need bi-directional HABTM, I ended up going back to creating individual tables.</p>