我正在创建一个应用程序,它将成为播客/广播节目的目录(主要是每个节目的展示)。我坚持如何在这个应用程序中模拟人物,主要是因为一个人可以两个在许多不同的剧集(在许多不同的节目中),但也可以是一个主持人(一个人)在每个特定节目的剧集中都有特色。)
示例:Marco Arment是节目“构建和分析”的主持人,但可能会偶尔出现在其他播客(例如“The Gruber Talk Show”或“本周的技术”)中。
到目前为止我的数据模型如下所示(专注于主持人/客人剧集/节目关系)。我不确定如何处理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或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
答案 0 :(得分:0)
你只谈论两种不同的模型,所以我会保持简单。我们将People
,Host
和Guest
合并到Person
;让我们将Episode
和Show
合并到Podcast
。您可以在其他位置定义主机和来宾之间的角色差异。
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
并在variety
Role
中存储Person
与Podcast
之间的关系类型。这可能是"host"
或"guest"
之类的字符串,或者您甚至可以创建另一个名为Variety
的模型来管理它(从而存储variety_id
而不是variety
在Role
)。
答案 1 :(得分:0)
show通过主机有很多用户
show有很多剧集
剧集有许多用户通过访客
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
答案 2 :(得分:0)
更好的选择是不要将Host和guest分开,Guest可以被定义为使用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