我开发一个游戏平台,我有以下的(简化)型号:包含在ActiveRecord的查询所有ID
class Game < ActiveRecord:Base
has_many :game_players
has_many :players, through: :game_players
end
class Player < ActiveRecord:Base
has_many :game_players
has_many :games, through: :game_players
end
class GamePlayer < ActiveRecord:Base
belongs_to :game
belongs_to :player
end
我需要执行一个ActiveRecord查询,查找由某一群玩过的所有游戏用户。例如,给定数据:
+---------+-----------+
| game_id | player_id |
+---------+-----------+
| 10 | 39 |
| 10 | 41 |
| 10 | 42 |
| 12 | 41 |
| 13 | 39 |
| 13 | 41 |
+---------+-----------+
我需要找到一种方法来确定哪些游戏是通过与IDS 39和41的球员,在这种情况下,将与IDS 10和13场比赛。我现在已经找到最多的查询是:
Game.joins(:players).where(players: {id: [39, 41]}).uniq
然而,这个查询返回,而不是由他们两人玩的游戏由任何这些玩家玩的游戏。
这是有效的,但是我想要执行一个单一的SQL查询,因为玩家的数量可以是任意大的 – Bustikiller
'Game.joins(:players).where(players:{id:39})。where(players :{id:41})' - 尝试这样的事情 – dp7