小编Aja*_*nda的帖子

Rails 3使用nested_has_many_through的复杂关联

我一直在努力开发一个基于电影的rails应用程序,它支持多个地区(好莱坞,宝莱坞等).我将多个区域称为应用程序中的语言.

每种语言都有自己的数据集,即英语中包含与好莱坞相关的所有电影,而语言印地语则包含与宝莱坞相关的所有电影.

语言模型

class Language < ActiveRecord::Base
  has_many :movies
  has_many :cast_and_crews, :through => :movies, :uniq => true
  has_many :celebrities, :through => :cast_and_crews, :uniq => true

  # FIXME: Articles for celebrities and movies 
  has_many :article_associations, :through => :celebrities
  has_many :articles, :through => :article_associations, :uniq => true
end
Run Code Online (Sandbox Code Playgroud)

这里的电影和名人都有使用article_association类的文章.

电影模型

class Movie < ActiveRecord::Base
  belongs_to :language
  has_many :cast_and_crews
  has_many :celebrities, :through => :cast_and_crews
  has_many :article_associations
  has_many :articles, :through => :article_associations, :uniq => true
end
Run Code Online (Sandbox Code Playgroud)

名人模特

class Celebrity < ActiveRecord::Base
  has_many :cast_and_crews
  has_many …
Run Code Online (Sandbox Code Playgroud)

scope named-scope ruby-on-rails associations ruby-on-rails-3

7
推荐指数
1
解决办法
1071
查看次数