7

저는 여러 지역 (할리우드, 볼리우드 등)을 지원하는 영화 기반 레일 애플리케이션을 개발하려고했습니다. 응용 프로그램에서 여러 영역을 언어로 호출합니다.Nails_has_many_through를 사용하는 레일즈 3의 복잡한 연결

각 언어마다 고유 한 데이터 세트가 있습니다. 즉, 영어에는 할리우드 관련 모든 영화가 있고 언어 힌디어에는 볼리우드 관련 모든 영화가 있습니다.

언어 모델 여기

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 

영화와 유명 인사들이 모두 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 

연예인 모델

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 < ActiveRecord::Base 
    has_many :article_associations 
    has_many :celebrities, :through => :article_associations 
    has_many :movies, :through => :article_associations 
end 

내가 달성하고자하는 것은 language.article이 유명인과 영화와 관련된 모든 기사를 반환해야합니다.

내가 SQL을 사용하지 않는 이유는 find_by_sql이 ActiveRelation을 지원하지 않기 때문에 has_scope 기능을 사용할 수 없기 때문입니다.

나는이에

어떤 도움이 크게 감사합니다 nested_has_many_through, has_scope 및 inherited_resources 보석을 사용하고 있습니다.

+0

, 언어가 조금 복잡와 같이 지역을 대표하는. 인도 영화가 영어로 된다면 어떨까요? 개념을 나눕니다. –

+0

벽을 이해했다면 중첩 된 has_many에서 문제가 발생하지 않습니다. 두 가지 소스 (영화 및 유명인)의 기사를 갖고 싶다는 사실에서부터입니다. 문제를 반대로 시도 했습니까? 언어에서 has_many 관계를 정의하지 않지만 Article에서 람다 범위를 정의합니까? 그것은 약간의 SQL을 포함 할 수 있습니다. – MrRuru

+0

@ MrRuru 당신 말이 맞아요. nested_has_many_through gem에 문제가 없습니다. 그것은 약속 한대로 행동합니다. 또한 내가 기사, 즉 영화 및 유명인을위한 여러 출처를 가지고 있다는 것도 맞습니다. 나는 SQL 기반 스코프가 Active Relation 인스턴스를 반환하지 않기 때문에 SQL 기반 스코프를 피하려고 노력하고 있는데, 이는 내가 사용하는 다른 플러그인, 즉 inherited_resources에 필요한 스코프를 연결할 수 없게된다. –

답변

0

ok이 문제를 해결했습니다.

추가이 나에게 내가 영화, 유명 인사 또는 이벤트에 대한 모든 기록을 검색하는 것으로 오전 액티브 :: 관계 인스턴스를 제공

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 

내 제 클래스의 다음과 같은 범위.

나를 도왔던 모든 분들께 감사드립니다.

개선 할 의견이 있으면 의견을 보내주십시오. 대단히 감사합니다.

1

Article의 외출은 주어진 언어 ID에 대한 모든 Movies를 조회 할 수 있습니다, 필요한 것을 허용해야 몇 가지 트릭이있다

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 

다음 언어로 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 

유일한 모르는 부분은 ... :article_movies은 SQL로 표현되는 방식이다

+0

. 나는 당신이 제안한 것을 시도했지만 "ActiveRecord :: ConfigurationError : Association이라는 'article_movies'를 찾을 수 없으므로 다음과 같은 오류가 발생합니다. 아마도 맞춤법 오류입니까? ". article_association 클래스에 article_movies가 없기 때문에 일어난 것 같아요 –

+0

ArticleAssociation에서'belongs_to : article_movie, : class => 'Movie'를 시도해야하지만 마이그레이션이 필요합니다 ('movie_id'에서'article_movie_id'로 키 변경 - 내 생각 엔 – mpapis

1

이제 레일 3.1은 중첩 관계를 지원합니다.물론 플러그인 후 더해야 하나 내장 : 보조 노트로

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

+0

너는 맞다. Mohammad. Rails 3.1에는 중첩 된 연관이있다. 나는이 플러그인을 오랫동안 사용하지 않을 것임을 알고있다. –

관련 문제