views:

97

answers:

1

There seems to be no name for this common model pattern.

It's used in many plugins like acts_as_taggable[_whatever] and it basically allows
linking a certain model, like Tag, with any other model without having to put
ever-more belongs_to statements in the Tag model.

It works by having your model (Tag) linked to a polymorphic join model (Tagging)
representing the join table. That creates a self-contained model in which any
other model can relate.
(They relate via a has_many :as & a has_many :through)

I often want to refer to this type of model relationship as one thing.
Maybe call it a "polylink model" or "polylinked model"?
Such as, "Make it a polylink model and relate it to any other models as you code."

Any other suggestions?

Here's the internal workings for acts_as_taggable models:

class Tag < ActiveRecord::Base
  has_many :taggings
end

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :taggable, :polymorphic => true
end

class Whatever < ActiveRecord::Base
  has_many :taggings, :as => :taggable, :dependent => :destroy
  has_many :tags, :through => :taggings
end

class CreateTaggings < ActiveRecord::Migration
  def self.up
    create_table :taggings do |t|
      t.references :tag
      t.references :taggable, :polymorphic => true
      t.timestamps
    end 
  end
end
+2  A: 

In Rails jargon I've seen this usually referred to as plain "has_many :through". With the polymorphism, "polymorphic has_many :through". Taking the Rails jargon out of it, I guess the general pattern could be called a "polymorphic many-to-many relationship".

obvio171
Look at has_many_polymorphs http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html
François Beausoleil