0

我正在創建一個應用程序,該應用程序將成爲播客/廣播節目的目錄(主要用於每個節目的展示)。我被困在如何在這個應用程序模型的人,主要是因爲一個人可以許多不同的情節(在許多不同的節目),但也可以是一個主機(每個插曲給定節目)。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 

回答

0

你只是在談論兩種不同的模型,所以我會保持簡單。我們將People,Host,Guest合併爲Person;我們將EpisodeShow合併爲Podcast。您可以在其他地方定義主機和guest虛擬機之間的角色差異。

class Person < ActiveRecord::Base 
    attr_accessible :name, :biography, :twitter 
    has_many :podcasts 
    has_many :podcasts, :through => :roles 
end 

class Podcast < ActiveRecord::Base 
    attr_accessible :title 
    has_many :persons 
    has_many :persons, :through => :roles 
end 

class Role < ActiveRecord::Base 
    attr_accessible :variety 
    belongs_to :podcast 
    belongs_to :person 
end 
Role PersonPodcast之間的關係的類型

,並存儲在variety。這可能是一個字符串,如"host""guest",或者甚至可以使用另一個模型Variety來管理(並因此在Role中存儲variety_id而不是variety)。

0

展現了通過主機多用戶

演出有許多情節

情節有過的客人很多用戶

class User < ActiveRecord::Base 
    has_many :shows, :through => :show_hosts 
    has_many :show_hosts 

    has_many :episodes, :through => :show_guests 
    has_many :show_guests 
end 
0

更好的選擇會不會已經賓主分開,來賓可以被定義爲使用HABTM的自我反思關係。

模型的定義會是這個樣子,

class Host < ActiveRecord::Base 
    has_and_belongs_to_many :guest, 
     :join_table => 'guests' #This is HABTM join table that you will have to create 
     :foreign_key => 'host_id' 
     :association_foreign_key => 'guest_id' 
     :class_name => 'Host' 


#The migration will look like this, 
def change 
    create_table :guests :id => false do |t| 
    t.integer :host_id, :null => false 
    t.integer :guest_id, :null => false 
    end 
    add_index :guests, [:host_id, :guest_id] 
end