首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Rails纠正电影网站的模型数据结构

使用Rails纠正电影网站的模型数据结构
EN

Stack Overflow用户
提问于 2011-01-12 01:26:48
回答 1查看 583关注 0票数 0

我需要拯救一群没有复制的电影相关的人。

让我们以电影Inglourious Basterds为例。

昆汀·塔伦蒂诺在这里有很多角色。

  • Director
  • Writer
  • Actor

下面是一个rspec测试的示例

代码语言:javascript
复制
Movie.find_by_title("Inglourious Basterds").actors.map(&:name).should include("Quentin Tarantino")
Movie.find_by_title("Inglourious Basterds").writers.map(&:name).should include("Quentin Tarantino")
Movie.find_by_title("Inglourious Basterds").directors.map(&:name).should include("Quentin Tarantino")

我应该如何建立模型之间的关系?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-12 02:07:49

这是rails要做的一组相当简单的模型。

代码语言:javascript
复制
rails g model movie name:string
rails g model person name:string
rails g model movie_role movie:belongs_to person:belongs_to role:string

对于示范协会而言:

代码语言:javascript
复制
class Person < ActiveRecord::Base
  has_many :movie_roles
  has_many :movies, :through => :movie_roles
end

class Movie < ActiveRecord::Base
  %w(actor director writer).each do |type|
    base = "#{type}_movie_roles"
    has_many base, :conditions => { :role => type }, :class_name => 'MovieRole'
    has_many type.pluralize, :through => base, :source => :person
  end
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4664559

复制
相关文章

相似问题

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