Я прочитал несколько вопросов об этом, но еще не нашел ответа, который работает для моей ситуации.
У меня есть 3 модели: Apps
, AppsGenres
и Genres
Вот соответствующие поля из каждого из них:
Apps
application_id
AppsGenres
genre_id
application_id
Genres
genre_id
Ключ здесь в том, что я не, используя поле id
из этих моделей.
Мне нужно связать таблицы на основе этих полей application_id
и genre_id
.
Вот то, что я получил в настоящее время, но мне не нужен запрос:
class Genre < ActiveRecord::Base
has_many :apps_genres, :primary_key => :application_id, :foreign_key => :application_id
has_many :apps, :through => :apps_genres
end
class AppsGenre < ActiveRecord::Base
belongs_to :app, :foreign_key => :application_id
belongs_to :genre, :foreign_key => :application_id, :primary_key => :application_id
end
class App < ActiveRecord::Base
has_many :apps_genres, :foreign_key => :application_id, :primary_key => :application_id
has_many :genres, :through => :apps_genres
end
Для справки, вот в чем я нуждаюсь в конечном итоге:
@apps = Genre.find_by_genre_id(6000).apps
SELECT "apps".* FROM "apps"
INNER JOIN "apps_genres"
ON "apps"."application_id" = "apps_genres"."application_id"
WHERE "apps_genres"."genre_id" = 6000