【发布时间】:2013-03-22 03:45:52
【问题描述】:
我正在创建一个应用程序,它将作为播客/广播节目的目录(主要是每个节目的节目说明)。我被困在如何在这个应用程序中为人物建模,主要是因为一个人可以在许多不同的剧集中(在许多不同的节目中)both作为嘉宾,但也可以是主持人(在出现在给定节目的每一集中)。
示例:Marco Arment 是“构建与分析”节目的主持人,但偶尔会作为嘉宾出现在其他播客(例如“格鲁伯脱口秀”或“本周科技”)中。
到目前为止,我的数据模型如下所示(重点关注主持人/嘉宾剧集/节目的关系)。我不确定如何处理 Person/Guest/Host 建模,但我确信我想将“Guest”和“Host”的角色作为单独的项目保留在应用程序中。
Episode
title:string
has_many :guests
Show
title:string
has_many :hosts
Host
show_id:integer
person_id:integer
belongs_to :show
Guest
episode_id:integer
person_id:integer
belongs_to :episode
People
name:string
# ???
我是否应该摆脱“主持人”和“客人”模型,而是根据剧集或节目是否询问与他们相关的人物来确定这些卷?请记住,“节目”只能有主持人,“剧集”只能有嘉宾——但嘉宾和主持人始终是人(姓名、传记、推特账号等)。
谢谢。我探索了多态关联和 has_many :through 并且不确定使用哪个,如果有的话。
更新 #1
睡了这个之后,我对如何在早上处理它有了一些更好的想法。与下面@Emm 的回复类似,“Host”和“Guest”应该只是通过单独模型的 Person 的某种品质,这似乎很自然。这是我对模型结构的新想法,我在看到以下任何回复之前就设置了它。
这里的主要区别在于“外观”模型将有一个“角色”列(字符串),我可以在其中设置与该行关联的 person_id 是该行的 episode_id 或 show_id 的“guest”还是“host” .
class Appearanceship
# person_id:integer
# episode_id:integer
# show_id:integer
# role:string
belongs_to :people
belongs_to :episodes
belongs_to :shows
end
class Person
# name, bio, profile pic, twitter name, etc
has_many :appearanceships
has_many :episodes, :through => :appearanceships
has_many :shows, :through => :appearanceships
end
class Episode
# title, summary, date recorded, mp3 URL, web URL, etc
has_many :appearanceships
has_many :people, :through => :appearanceships
end
class Show
# title, description, web URL, RSS URL, etc
has_many :appearanceships
has_many :people, :through => :appearanceships
end
class Network
# e.g., "NPR", "5by5", "Mule Radio Syndicate", "Revision3"
# title, description, web URL, etc
has_many :shows
end
【问题讨论】:
标签: ruby-on-rails has-many-through polymorphic-associations model-associations podcast