我正在創建一個應用程序,該應用程序將成爲播客/廣播節目的目錄(主要用於每個節目的展示)。我被困在如何在這個應用程序模型的人,主要是因爲一個人可以許多不同的情節(在許多不同的節目),但也可以是一個主機(每個插曲給定節目)。Podcast目錄(Polymoprhic或HABTM)的Rails Model Association
例如:Marco Arment是「構建和分析」節目的主持人,但偶爾會出現其他播客(例如「The Gruber Talk Show」或「This Week in Tech」)。
我的數據模型到目前爲止顯示如下(側重於主持人/嘉賓情節/表演關係)。我不確定如何處理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
# ???
我應該擺脫「主」和「客」車型,而是基於確定是否劇集或節目都是詢問與之相關的人,這些卷?記住,「Show」只能有主人,而「Episode」只能有賓客 - 但客人和主人總是人(姓名,傳記,嘰嘰喳喳句柄等)。
謝謝。我探索了多態關聯和has_many:通過並且不確定使用哪一個,如果有的話。
更新#1
睡在這之後,我對如何處理它在早晨一些更好的想法。與下面的@ Emm的回答類似,「主持人」和「客人」應該通過一個單獨的模型簡單地成爲某人的某種品質,這似乎很自然。這是我對模型結構的新思考,我在看到下面的任何回答之前就已經設置好了。
這裏的關鍵區別在於,「Appearanceship」模型會有一個「角色」列(字符串),我可以設置與該行關聯的person_id是該行的episode_id的「guest」還是「host」 show_id。
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