rails g model Article name:string
rails g model Category name:string
rails g model Tag name:string taggable_id:integer taggable_type:string category_id:integer
我已經創建了我的模型,如上面的代碼所示。文章將是可以有標籤的許多模型之一。類別模型將包含可能分配的所有類別。標記模型將是一個多態連接表,它表示標記關係。設置多態has_many:通過關係
class Article < ActiveRecord::Base
has_many :tags, :as => :taggable
has_many :categories, :through => :taggable
end
class Category < ActiveRecord::Base
has_many :tags, :as => :taggable
has_many :articles, :through => :taggable
end
class Tag < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
belongs_to :category
end
我似乎無法得到這個工作,我能做到這一點非多態的,但我必須有一些錯誤的多態性的一部分。有任何想法嗎?
編輯:仍然沒有得到這一權利:
class Article < ActiveRecord::Base
has_many :taggables, :as => :tag
has_many :categories, :through => :taggables, :source => :tag, :source_type => "Article"
end
class Category < ActiveRecord::Base
has_many :taggables, :as => :tag
has_many :articles, :through => :taggables, :source => :tag, :source_type => "Article"
end
class Tag < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
belongs_to :category
end
去試試看今天有點看看我是否完全理解如何做到這一點。 – Serodis 2011-05-05 12:28:25