本文介绍了的has_many:通过与外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过有关这多个问题,但还没有找到适合我的情况问题的解答。

我有3个型号:应用 AppsGenres 流派

下面是相关领域从每次的:

 应用程序
APPLICATION_IDAppsGenres
genre_id
APPLICATION_ID流派
genre_id

这里的关键是从这些模型,我的不可以使用 ID 字段。

我需要根据这些表关联 APPLICATION_ID genre_id 字段。

下面就是我目前得到的,但它没有得到我,我需要查询:

 类体裁< ActiveRecord的::基地
  的has_many:apps_genres,:primary_key => :APPLICATION_ID,:foreign_key => :APPLICATION_ID
  的has_many:应用程序,:通过=> :apps_genres
结束类AppsGenre< ActiveRecord的::基地
  belongs_to的:应用程序,:foreign_key => :APPLICATION_ID
  belongs_to的:体裁,:foreign_key => :APPLICATION_ID,:primary_key => :APPLICATION_ID
结束类应用< ActiveRecord的::基地
  的has_many:apps_genres,:foreign_key => :APPLICATION_ID,:primary_key => :APPLICATION_ID
  的has_many:流派:通过=> :apps_genres
结束

有关参考,在这里是查询我最终需要:

  @apps = Genre.find_by_genre_id(6000).apps选择应用程序* FROM应用程序
   INNER JOINapps_genres
      ON应用程序。APPLICATION_ID=apps_genres。APPLICATION_ID
   WHEREapps_genres。genre_id= 6000


解决方案

更新时间:试试这个:

 类应用< ActiveRecord的::基地
  的has_many:apps_genres,:foreign_key => :APPLICATION_ID
  的has_many:流派:通过=> :apps_genres
结束类AppsGenre< ActiveRecord的::基地
  belongs_to的:体裁,:foreign_key => :genre_id,:primary_key => :genre_id
  belongs_to的:应用程序,:foreign_key => :APPLICATION_ID,:primary_key => :APPLICATION_ID
结束类体裁< ActiveRecord的::基地
  的has_many:apps_genres,:foreign_key => :genre_id
  的has_many:应用程序,:通过=> :apps_genres
结束

通过查询:

  App.find(1).genres

它产生:

  SELECT`genres`。* FROM`genres` INNER JOIN`apps_genres`开`genres`.`genre_id` =`apps_genres`.`genre_id`其中`apps_genres`.`application_id `= 1

和查询:

  Genre.find(1).apps

生成:

  SELECT`apps`。* FROM`apps` INNER JOIN`apps_genres`开`apps`.`application_id` =`apps_genres`.`application_id`其中`apps_genres`.`genre_id `= 1

I've read multiple questions about this, but have yet to find an answer that works for my situation.

I have 3 models: Apps, AppsGenres and Genres

Here are the pertinent fields from each of those:

Apps
application_id

AppsGenres
genre_id
application_id

Genres
genre_id

The key here is that I'm not using the id field from those models.

I need to associate the tables based on those application_id and genre_id fields.

Here's what I've currently got, but it's not getting me the query I need:

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

For reference, here is the query I ultimately need:

@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
解决方案

UPDATED Try this:

class App < ActiveRecord::Base
  has_many :apps_genres, :foreign_key => :application_id
  has_many :genres, :through => :apps_genres
end

class AppsGenre < ActiveRecord::Base
  belongs_to :genre, :foreign_key => :genre_id, :primary_key => :genre_id
  belongs_to :app, :foreign_key => :application_id, :primary_key => :application_id
end

class Genre < ActiveRecord::Base
  has_many :apps_genres, :foreign_key => :genre_id
  has_many :apps, :through => :apps_genres
end

With query:

App.find(1).genres

It generates:

SELECT `genres`.* FROM `genres` INNER JOIN `apps_genres` ON `genres`.`genre_id` = `apps_genres`.`genre_id` WHERE `apps_genres`.`application_id` = 1

And query:

Genre.find(1).apps

generates:

SELECT `apps`.* FROM `apps` INNER JOIN `apps_genres` ON `apps`.`application_id` = `apps_genres`.`application_id` WHERE `apps_genres`.`genre_id` = 1

这篇关于的has_many:通过与外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:23