Hmmm... Silence again...? What gives SO? Hello...? Bueller?
Never fear, here's the answer:
After looking into Single Table Inheritance (not the answer, but an interesting technique for other slightly-related problems), I stumbled across a SO question regarding multiple-references to a polymorphic association on the same model. (Thank you hakunin for your detailed answer, +1.)
Basically we need to explicitly define the contents of the taggable_type
column in the Taggings
table for the sub_taggings
association, but not with source
or source_type
, instead with :conditions
.
The Article
model shown below now passes all the tests:
class Article < ActiveRecord::Base
has_many :taggings, as: :taggable
has_many :tags, through: :taggings, uniq: true, dependent: :destroy
has_many :sub_taggings, as: :taggable, class_name: 'Tagging',
conditions: {taggable_type: 'article_sub_tag'},
dependent: :destroy
has_many :sub_tags, through: :sub_taggings, class_name: 'Tag',
source: :tag, uniq: true
end
UPDATE:
This is the correct Tag
model that produces functional reverse polymorphic associations on Tags. The reverse association (ie. Tag.articles and Tag.sub_tagged_articles) passes tests.
class Tag < ActiveRecord::Base
has_many :articles, through: :taggings, source: :taggable,
source_type: "Article"
has_many :sub_tagged_articles, through: :taggings, source: :taggable,
source_type: "Article_sub_tag", class_name: "Article"
end
I have also extended and successfully tested the schema to allow tagging & sub_tagging of other models using the same Tag model and Tagging join table. Hope this helps someone.