Ruby on rails Rails:对来自前一个响应的每个响应的新查询
我有以下“注册”表: 一名球员可以参加多场比赛(如图所示,球员12参加了比赛59) 现在我想展示所有注册了与当前玩家相同游戏的玩家 所以我想我必须:Ruby on rails Rails:对来自前一个响应的每个响应的新查询,ruby-on-rails,Ruby On Rails,我有以下“注册”表: 一名球员可以参加多场比赛(如图所示,球员12参加了比赛59) 现在我想展示所有注册了与当前玩家相同游戏的玩家 所以我想我必须: 捕获所有比赛\u id,其中player\u id=当前\u player 抓住每场比赛中的所有球员 这是好办法吗?还是我应该知道一些神奇的东西?我该怎么做? 谢谢你的帮助 更新 我试过这个: @matchs_du_joueur = Registrations.where(:player_id => current_user.id)
player\u id=当前\u player
@matchs_du_joueur = Registrations.where(:player_id => current_user.id)
@joueurs = Player.joins(:registrations).where(:registrations => { :match_id => @matchs_du_joueur.match_id })
我有一个错误:
undefined method `match_id' for #<ActiveRecord::Relation:0x6d8a3b0>
未定义的方法“match_id”#
但是,我已经定义了所有的has\u many\u和\u所属的。
不知道怎么做 是的,就是这样。我想
class Match < ActiveRecord::Base
has_many :registrations
has_many :players, through: :registrations
end
class Player < ActiveRecord::Base
has_many :registrations
has_many :matches, through: :registrations
end
class Registration < ActiveRecord::Base
belongs_to :match
belongs_to :player
end
谢谢你的回复,但我无法让它工作。我将更新我的问题。
@matchs_du__jouer
包含用户拥有的所有注册的列表。如果您使用@matchs_du_jouer.map(&:match_id)
,应该可以了。非常感谢您的帮助,它可以工作!
Player.joins(:registrations).where(registrations: { match_id: current_player.match_ids })