Ruby on rails Rails 3使用嵌套\u的复杂关联有\u多个\u到

Ruby on rails Rails 3使用嵌套\u的复杂关联有\u多个\u到,ruby-on-rails,ruby-on-rails-3,scope,associations,named-scope,Ruby On Rails,Ruby On Rails 3,Scope,Associations,Named Scope,我一直在尝试开发一个基于电影的rails应用程序,它支持多个地区(好莱坞、宝莱坞等)。我将多个区域称为应用程序中的语言 每种语言都有自己的数据集,即英语有与好莱坞相关的所有电影,印地语有与宝莱坞相关的所有电影 语言模型 class Language < ActiveRecord::Base has_many :movies has_many :cast_and_crews, :through => :movies, :uniq => true has_many :c

我一直在尝试开发一个基于电影的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
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
class Celebrity < ActiveRecord::Base
  has_many :cast_and_crews
  has_many :movies, :through => :cast_and_crews, :uniq => true
  has_many :article_associations
  has_many :articles, :through => :article_associations, :uniq => true
end

class ArticleAssociation < ActiveRecord::Base
  belongs_to :article
  belongs_to :celebrity
  belongs_to :movie
end
类语言:movies,:uniq=>true
有很多:名人,:通过=>:演员和工作人员,:uniq=>真的
#FIXME:名人和电影的文章
有很多:文章协会,:至=>:名人
有很多:文章,:至=>:article\u关联,:uniq=>true
结束
这里电影和名人都有使用article_association类的文章

电影模式

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
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
class Celebrity < ActiveRecord::Base
  has_many :cast_and_crews
  has_many :movies, :through => :cast_and_crews, :uniq => true
  has_many :article_associations
  has_many :articles, :through => :article_associations, :uniq => true
end

class ArticleAssociation < ActiveRecord::Base
  belongs_to :article
  belongs_to :celebrity
  belongs_to :movie
end
class电影:演员和工作人员
有很多:文章协会
有很多:文章,:至=>:article\u关联,:uniq=>true
结束
名人模特

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
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
class Celebrity < ActiveRecord::Base
  has_many :cast_and_crews
  has_many :movies, :through => :cast_and_crews, :uniq => true
  has_many :article_associations
  has_many :articles, :through => :article_associations, :uniq => true
end

class ArticleAssociation < ActiveRecord::Base
  belongs_to :article
  belongs_to :celebrity
  belongs_to :movie
end
class:演员和工作人员,:uniq=>真实
有很多:文章协会
有很多:文章,:至=>:article\u关联,:uniq=>true
结束
类ArticleAssociation
这就是我的文章模型的定义

class Article < ActiveRecord::Base
  has_many :article_associations
  has_many :celebrities, :through => :article_associations
  has_many :movies, :through => :article_associations
end
类文章:article\u协会
有很多:电影,:通过=>:文章
结束
我想要达到的是语言。文章应该返回所有与名人和电影相关的文章

我不使用SQL的原因是find_by_SQL不支持ActiveRelation,我将无法使用has_scope功能

我正在使用嵌套的\u has \u many \u through、has \u scope和继承的\u资源gems


在这方面的任何帮助都将不胜感激。

有一些技巧可以满足您的需要,走出
文章
您可以查询所有
电影
,以获得给定的语言id

class Article < ActiveRecord::Base
  has_many :article_associations
  has_many :celebrities, :through => :article_associations
  has_many :article_movies, :through => :article_associations, :class => 'Movie'
  scope :for_language, lambda {|lang_id| 
    joins(
      :article_associations=>[ 
        :article_movies, 
        {:celebrities => { :cast_and_crews => :movies } } 
      ]
    ).where(
      'movies.language_id = ? OR article_movies.language_id = ?', 
      lang_id, lang_id
    ) 
  } 
end

这里唯一不确定的部分是如何在sql中表示
:article\u movies

好的,这就是我为解决这个问题所做的

在我的文章类中添加了以下范围

def self.region(region_id)
  joins(<<-eos
    INNER JOIN
    (
      SELECT DISTINCT aa.article_id
      FROM regions r
           LEFT JOIN movies m on m.region_id = r.id
           LEFT JOIN cast_and_crews cc on cc.movie_id = m.id
           LEFT JOIN celebrities c on c.id = cc.celebrity_id
           LEFT JOIN events e on e.region_id = r.id
           LEFT JOIN article_associations aa on (aa.event_id = e.id or aa.movie_id = m.id or aa.celebrity_id = c.id)
      WHERE r.id = #{region_id}
    ) aa
  eos
  ).where("aa.article_id = articles.id")
end
def self.region(region_id)

连接(Rails3.1现在支持嵌套关系。当然内置的应该比插件更好:)


作为旁注,将区域表示为语言有点复杂。如果一部印度电影是英文的呢?分开概念。如果我理解墙,你的麻烦不是来自嵌套的has\u many:through(你有它的宝石),而是来自你想要两个来源的文章(电影和名人)?你试过扭转这个问题吗?不是在语言中定义了很多关系,而是在文章中定义了lambda范围?不过,这可能会涉及一些SQL。@MrRuru您是对的。我对nested_没有问题,它通过gem有很多个_。它承诺什么就做什么。你说得对,我有很多文章来源,比如电影和名人。我试图避免使用基于SQL的作用域,因为基于SQL的作用域不会返回一个活动的关系实例,并且我将无法链接我使用的其他插件所需的作用域,即继承的_资源。感谢@matthew的建议。为了调查此事,我将语言重命名为region。我尝试了您的建议,但在尝试“ActiveRecord::ConfigurationError:找不到名为“article_movies”的关联;可能是您拼错了?”时出现了以下错误。我猜这是因为我在article\u association类中没有article\u电影。您应该尝试在article\u association中
属于:article\u电影,:class=>“电影”
,但这需要迁移(将密钥从
movie\u id
更改为
article\u movie\u id
——我想你是对的,Mohammad。我知道Rails 3.1有嵌套的关联。我知道我不会使用这个插件很长时间。