【发布时间】:2014-03-18 18:41:32
【问题描述】:
如何在 Rails 中进行这个 SQL 查询?
我正在尝试加入三个表。我很确定我已经在模型中正确连接了它们。
我有几个连接表,在这种情况下,有一个“所有权”表,其中包含用户 ID 和游戏 ID。它与“游戏”表和“用户”表进行内连接。游戏表有游戏信息,并有一个与控制台表连接的console 列。控制台表是数据库中的所有控制台。这样,当我展示一个游戏的时候,我就可以展示它所属的控制台了。
我创建了一个网络界面来跟上我的视频游戏收藏。我让它在 PHP 中工作,但我正在将所有内容都转换为 Ruby on Rails。我现在要做的是,在库存页面上,我想显示用户拥有游戏的控制台,以便用户可以选择一个控制台并过滤显示的游戏以仅显示该控制台的游戏。
例如,主库存页面会显示用户拥有的每款游戏。右侧有一个侧边栏,其中包含用户拥有游戏的控制台。如果用户只想查看他拥有的 N64 游戏,他点击 N64 并过滤显示以仅显示 N64 游戏。
我不想在侧边栏显示控制台,除非用户拥有该控制台的游戏。
这是我想要处理的 SQL 查询:
select distinct console_general.eng_name from ownership inner join games on games.id=ownership.games_id inner join console_general on console_general.console_id=games.console_general_id;
这是我的桌子:
mysql> describe games;
+--------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ean | mediumtext | YES | | NULL | |
| eng_title | mediumtext | YES | | NULL | |
| jap_title | mediumtext | YES | | NULL | |
| console_general_id | int(11) | YES | MUL | NULL | |
| region_id | int(11) | YES | MUL | NULL | |
| image | int(11) | YES | MUL | NULL | |
+--------------------+------------+------+-----+---------+----------------+
mysql> describe ownership;
+----------------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------+------+-----+---------+-------+
| user_id | int(11) | NO | | 0 | |
| games_id | int(11) | YES | MUL | NULL | |
| own | tinyint(1) | YES | | NULL | |
| complete | tinyint(1) | YES | | NULL | |
| box_condition | int(11) | YES | MUL | NULL | |
| game_condition | int(11) | YES | MUL | NULL | |
| manual_condition | int(11) | YES | MUL | NULL | |
| inserts_condition | int(11) | YES | MUL | NULL | |
| notes | text | YES | | NULL | |
| spine_card_condition | int(11) | YES | MUL | NULL | |
| count | int(11) | NO | | 1 | |
+----------------------+------------+------+-----+---------+-------+
mysql> describe console_general
-> ;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| console_id | int(11) | NO | PRI | NULL | auto_increment |
| eng_name | text | YES | | NULL | |
| jap_name | text | YES | | NULL | |
+------------+---------+------+-----+---------+----------------+
这是我的控制器:
class InventoryController < ApplicationController
def test
# These work fine
@user = User.find_by(params[:id])
@ownership = Ownership.where(user_id: 1)
# This is the variable I've been working on, not sure if I'm on the right track or not.
# @console = Ownership.joins(games: {console_general: :console_id})
end
end
这是我的模型:
class Ownership < ActiveRecord::Base
self.pluralize_table_names = false
belongs_to :games
Ownership.joins(:games)
end
class Games < ActiveRecord::Base
belongs_to :console_general
belongs_to :region
belongs_to :image
has_many :ownership
Games.joins(:region, :console_general, :image)
end
lass ConsoleGeneral < ActiveRecord::Base
self.pluralize_table_names = false
has_many :games
has_many :accessories
end
我希望这是有道理的。
编辑:
稍微处理一下给定的答案,我就能得到它。模型关联是正确的,但控制器实例变量一开始不起作用,这就是我所做的
Ownership.joins(:games => :console_general).uniq.pluck(:eng_name)
这很好用。感谢大家的帮助。
【问题讨论】:
标签: sql ruby-on-rails ruby join controller