저는 여러 지역 (할리우드, 볼리우드 등)을 지원하는 영화 기반 레일 애플리케이션을 개발하려고했습니다. 응용 프로그램에서 여러 영역을 언어로 호출합니다.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 보석을 사용하고 있습니다.
, 언어가 조금 복잡와 같이 지역을 대표하는. 인도 영화가 영어로 된다면 어떨까요? 개념을 나눕니다. –
벽을 이해했다면 중첩 된 has_many에서 문제가 발생하지 않습니다. 두 가지 소스 (영화 및 유명인)의 기사를 갖고 싶다는 사실에서부터입니다. 문제를 반대로 시도 했습니까? 언어에서 has_many 관계를 정의하지 않지만 Article에서 람다 범위를 정의합니까? 그것은 약간의 SQL을 포함 할 수 있습니다. – MrRuru
@ MrRuru 당신 말이 맞아요. nested_has_many_through gem에 문제가 없습니다. 그것은 약속 한대로 행동합니다. 또한 내가 기사, 즉 영화 및 유명인을위한 여러 출처를 가지고 있다는 것도 맞습니다. 나는 SQL 기반 스코프가 Active Relation 인스턴스를 반환하지 않기 때문에 SQL 기반 스코프를 피하려고 노력하고 있는데, 이는 내가 사용하는 다른 플러그인, 즉 inherited_resources에 필요한 스코프를 연결할 수 없게된다. –