0
似乎沒有這種常見模型的名稱。這個Ruby on Rails通用模型模式有一個名字嗎? Polylink?
它在許多插件,像acts_as_taggable[_whatever]
使用,它基本上可以讓
連接一定的模型,如標籤,與其他型號,而不必把
不斷更belongs_to的聲明中所述標籤模型。
它通過讓您的模型(標記)鏈接到表示連接表的多態聯接模型(標記)
來工作。這創建了一個獨立的模型,其中任何其他模型都可以與之相關。
(他們通過has_many
涉及:作爲一個&的has_many:通過)
我經常要引用這種類型的模型關係,一兩件事。
也許稱它爲「多鏈接模型」或「多鏈接模型」?
例如,「使其成爲多鏈接模型並在您編碼時將其與任何其他模型關聯。」
其他建議?
這裏是內部工作的acts_as_taggable
型號:
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
看看has_many_polymorphs http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html –