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