title: Rails model with two polymorphic has_many through: associations for object tagging
body: <p>My schema has <code>Articles</code> and <code>Journals</code> that can be tagged with <code>Tags</code>. This requires a <code>has_many through:</code> association with a polymorphic relationship to my <code>Tagging</code> join table.</p>
  
  <p>Okay, that's the easy and well documented part.</p>
  
  <p>My problem is that <code>Articles</code> can have both primary-tags and sub-tags. The primary tags are what I'm most interested in, but my model also needs to keep track of these sub tags. Sub tags are simply labels describing the <code>Article</code> that are of lesser importance, but come from the same global pool of <code>Tags</code>. (in fact, one <code>Article</code>'s primary-tag may be another's sub-tag).</p>
  
  <p>Achieving this requires the <code>Article</code> model to have two associations to the <code>Tagging</code> model and two <code>has_many through:</code> associations to <code>Tags</code> (i.e. #tags &amp; #sub-tags)</p>
  
  <p>This is what I have so far, which while valid does not keep primary-tags and sub-tags separate.</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
    has_many :taggings, as: :taggable
    has_many :tags, through: :taggings
  
    has_many :sub_taggings, as: :taggable, class_name: 'Tagging',
             source_type: 'article_sub'
    has_many :sub_tags, through: :sub_taggings, class_name: 'Tag', source: :tag
  end
  
  class Tagging &lt; ActiveRecord::Base
    #  id            :integer
    #  taggable_id   :integer
    #  taggable_type :string(255)
    #  tag_id        :integer
    belongs_to :tag
    belongs_to :taggable, :polymorphic =&gt; true
  end
  
  class Tag &lt; ActiveRecord::Base
    has_many :taggings
  end
  </code></pre>
  
  <p>I know that somewhere in there I need to find the right combination of <code>source</code> and <code>source_type</code> but I can't work it out.</p>
  
  <p>For completeness here is my <code>article_spec.rb</code> that I'm using to test this — currently failing on "the incorrect tags".</p>
  
  <pre><code>describe "referencing tags" do
    before do
      @article.tags &lt;&lt; Tag.find_or_create_by_name("test")
      @article.tags &lt;&lt; Tag.find_or_create_by_name("abd")
      @article.sub_tags &lt;&lt; Tag.find_or_create_by_name("test2")
      @article.sub_tags &lt;&lt; Tag.find_or_create_by_name("abd")
    end
  
    describe "the correct tags" do
      its(:tags) { should include Tag.find_by_name("test") }
      its(:tags) { should include Tag.find_by_name("abd") }
      its(:sub_tags) { should include Tag.find_by_name("abd") }
      its(:sub_tags) { should include Tag.find_by_name("test2") }
    end
  
    describe "the incorrect tags" do
      its(:tags) { should_not include Tag.find_by_name("test2") }
      its(:sub_tags) { should_not include Tag.find_by_name("test") }
    end
  end
  </code></pre>
  
  <p>Thanks in advance for any help on achieving this. The main problem is I cannot work out how to tell Rails the source_type to use for the sub_tags association in Articles.</p>