首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Podcast目录的Rails模型关联(Polymoprhic或HABTM)

Podcast目录的Rails模型关联(Polymoprhic或HABTM)
EN

Stack Overflow用户
提问于 2013-03-22 03:45:52
回答 3查看 160关注 0票数 0

我正在创建一个应用程序,它将成为播客/广播节目的目录(主要用于每个节目的展示笔记)。我被困在如何在这个应用中建模的人,主要是因为一个人既可以在许多不同的插曲(在许多不同的节目)的客人,但也可以是一个主持人(谁是一个特定节目的每一集的人)。

马可·阿蒙是“构建与分析”节目的主持人,但他可能会偶尔出现在其他播客上(比如“格鲁伯脱口秀”( the Gruber Talk show )或“本周科技”)。

到目前为止,我的数据模型如下所示(集中于主机/来宾集/显示关系)。我不知道如何接近Person/Guest/Host,虽然我确信我希望将“来宾”和“主机”的角色保留为应用程序中的单独项。

代码语言:javascript
复制
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 :通过,并且不确定使用哪一个,如果两者都是。

更新#1

睡过觉后,我有了一些更好的主意,如何在早上处理它。就像下面@Emm的回答一样,“主人”和“客人”仅仅是通过一个单独的模型来表现一个人的某种品质,这似乎是很自然的。这是我对模型结构的新想法,在看到下面的任何反应之前,我都建立了模型结构。

这里的关键区别在于,“Ex工程化”模型将有一个“角色”列(string),如果与该行关联的person_id是该行的episode_id或show_id的“来宾”或“宿主”,则可以设置该列的角色。

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

发布于 2013-03-22 04:04:47

你说的只是两种不同的模式,所以我会保持简单。让我们将PeopleHostGuest组合成Person,并将EpisodeShow合并到Podcast中。您可以在其他地方定义主机和来宾之间的角色差异。

代码语言:javascript
复制
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中,PersonPodcast之间的关系类型。这可能是一个类似于"host""guest"的字符串,或者您甚至可以创建另一个名为Variety的模型来管理这个模型(从而在Role中存储variety_id而不是variety )。

票数 0
EN

Stack Overflow用户

发布于 2013-03-22 04:05:33

节目通过主机有许多用户。

节目有很多集

剧集有很多用户通过客人。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-03-22 04:08:02

更好的选择将是不让主人和客人分开,客人可以被定义为使用HABTM的自我折射关系。

你的模型定义会像这样,

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15562380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档