Rails 3 complex associations using nested_has_many_through
Asked Answered
S

3

7

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_associations, :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.

Stokowski answered 4/5, 2011 at 5:31 Comment(4)
As a side note, representing region as language is a bit convoluted. What if an Indian film is in English? Split the concepts.Assamese
If I understand wall, your trouble doesn't come from the nested has_many :through (you have the gem for it), but from the fact that you want to have the articles from 2 sources (the movies and the celebrities)? Have you tried reversing the problem? Not defining a has_many relationship in Language, but defining a lambda scope in Article? It might involve a bit of SQL though.Gelhar
@Gelhar you are right. I don't have a problem with nested_has_many_through gem. It does what it promises. Also you are right that I have multiple sources for articles i.e., movies and celebrities. I am trying to avoid SQL based scope as SQL based scope does not return me a Active Relation instance and I won't be able to chain the scopes which is required for other plugin that I use i.e., inherited_resources.Stokowski
Thanks @matthew for the suggestion. I had renamed language to region.Stokowski
S
0

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.

Stokowski answered 20/5, 2011 at 1:50 Comment(0)
T
2

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

Teahan answered 21/5, 2011 at 19:19 Comment(1)
Ya you are right Mohammad. I know that Rails 3.1 has nested association. I know I will not be using this plugin for very long time.Stokowski
K
1

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 ...

Kylix answered 17/5, 2011 at 1:42 Comment(2)
for looking into this. I tried what you had suggested but I get the following error when I try "ActiveRecord::ConfigurationError: Association named 'article_movies' was not found; perhaps you misspelled it? ". I guess its happening because I do not have article_movies in article_association classStokowski
you should try belongs_to :article_movie, :class=>'Movie' in ArticleAssociation, but that will require migration (changing key from movie_id to article_movie_id - I guess.Kylix
S
0

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.

Stokowski answered 20/5, 2011 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.