Rails 3 сложные ассоциации с использованием nested_has_many_through

Я пытался разработать приложение 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

Здесь и фильмы, и знаменитости имеют статьи, использующие класс 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

Любая помощь в этом будет очень признателен.

7
задан Ajay Kumar Guthikonda 20 May 2011 в 01:52
поделиться