【问题标题】:Rails Model Association for a Podcast directory (Polymoprhic or HABTM)播客目录的 Rails 模型关联(Polymoprhic 或 HABTM)
【发布时间】: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


    【解决方案1】:

    您只是在谈论两种不同的模型,所以我会保持简单。让我们将PeopleHostGuest 组合成Person;让我们将EpisodeShow 组合成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
    

    并在Role 中存储varietyPersonPodcast 之间的关系类型。这可能是像"host""guest" 这样的字符串,或者您甚至可以创建另一个名为Variety 的模型来管理它(从而将variety_id 存储在Role 中而不是variety)。

    【讨论】:

      【解决方案2】:

      节目通过主机有很多用户

      节目有很多集

      剧集通过客人有很多用户

      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
      

      【讨论】:

        【解决方案3】:

        更好的选择是不将 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多