【发布时间】:2014-03-12 03:32:09
【问题描述】:
我正在开发一个 Ruby on Rails 4 应用程序,它允许用户检查他们看过的电影。我有一个页面显示通过Film 模型捕获的电影列表。另外,我有一个 Nomination 模型,它可以捕获每个奥斯卡提名,还有 Person 和 Song 模型,它们也可以获得提名并属于 Film。
在列出电影的页面上,我还想显示电影被提名的所有类别。我知道我可以通过 @film.nominations 轻松访问所有电影提名,因为我创建了这个 has_many 关联,但这并没有捕获属于该特定电影的任何人或歌曲提名。
有没有办法显示给定电影的所有提名,无论提名是针对电影、人物还是歌曲?
以下是我目前拥有的模型:
class Category < ActiveRecord::Base
# DB columns: name, display_order
has_many :nominations
end
class Nomination < ActiveRecord::Base
# DB columns: category_id, nominee_id, nominee_type
belongs_to :category
belongs_to :nominee, polymorphic: true
validates :category, presence: true
validates :nominee, presence: true
end
class Film < ActiveRecord::Base
# DB columns: name, image_url
has_many :nominations, as: :nominee
has_many :film_views
has_many :people
has_many :songs
end
class Person < ActiveRecord::Base
# DB columns: name, film_id
belongs_to :film
has_many :nominations, as: :nominee
end
class Song < ActiveRecord::Base
# DB columns: name, film_id
belongs_to :film
has_many :nominations, as: :nominee
end
【问题讨论】:
-
是电影和电影是两个不同的模型
-
不,它们是同一型号。我只是互换使用这些术语。
标签: ruby-on-rails postgresql activerecord