开发者

Rails 3 complex associations using nested_has_many_through

I have been trying to develop a movie based rails application which has support for multiple regions (Hollywood, Bollywood etc). I call the multiple regions as languages in the application.

Each language has its own set of data i.e., english has all the movies related to hollywood and language hindi has all the movies related to bollywood.

Language Model

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_associati开发者_Python百科ons, :uniq => true
end

Here movies and celebrities both have articles using the article_association class.

Movie Model

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

Celebrity Model

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

and this is how my Article model is defined

class Article < ActiveRecord::Base
  has_many :article_associations
  has_many :celebrities, :through => :article_associations
  has_many :movies, :through => :article_associations
end

What I am trying to achieve is language.article should return all the articles related to celebrities and movies.

The reason why I am not using SQL is find_by_sql does not support ActiveRelation and I will not be able use has_scope functionality.

I am using nested_has_many_through, has_scope and inherited_resources gems

Any help in this will be greatly appreciated.


Rails 3.1 now has support for nesting relations. Of course the built in one should be better then a plugin :)

http://railscasts.com/episodes/265-rails-3-1-overview


There are few tricks that should allow what you need, going out of Article you can query all the Moviesfor given language 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

Then in language define a method that will use earlier scope of Article

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

  def articles
    Article.for_language id
  end
end

The only unsure part here is how :article_movies will be represented in sql ...


ok This is what I did to fix this.

Added the following scope in my Article class

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

This gives me a ActiveRecord::Relation instance that I am expected which retrieves all the records for a movie, celebrity or event.

Thanks for all who helped me.

If you have any comments to improve it please comment it. Very much appreciated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜