A Video
, aSong
和 anArticle
可以有很多Tags
。而且每一个Tag
也可以有很多Video, Songs or Articles
。所以我有5个模型:Video, Song, Article, Tag and Taggings
.
以下是这些模型:
class Video < ActiveRecord::Base
has_many :tags, :through => :taggings
end
class Song < ActiveRecord::Base
has_many :tags, :through => :taggings
end
class Article < ActiveRecord::Base
has_many :tags, :through => :taggings
end
class Tag < ActiveRecord::Base
has_many :articles
has_many :videos
has_many :songs
belong_to :taggings, :polymorphic => true #is this correct?
end
数据库定义Taggings
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
Taggings
模型:
class Taggings < ActiveRecord::Base
belongs_to :tag #is this correct?
belong_to :taggable, :polymorphic => true #is this correct?
end
我担心的问题是,我对模型 ( belongs_to
, has_many
?) 有正确的定义吗?我的直觉告诉我,我错过了一些东西。我看过很多文章,我对它们感到很困惑。