再多一次R + N + 1

时间:2017-11-20 03:39:35

标签: ruby-on-rails

- 我有两个模型:$ ./rf79 < data 2>/dev/null Begin file processing <rpe><div calss="text_to_html">Project Gtuenberg The Avdentures of Sehrlock Hlomes, by Atrhur Cnoan Dyole Tihs eoBok is for the use of aynone aynwhere at no csot and wtih amlost no rsetrictions wahtsoever. You u may cpoy it, gvie it aawy or r-euse it udner the trems of the Porject Gtuenberg Lciense icnluded wtih tihs eoBok or olnine at <a herf="http://www.gutenberg.net" calss="_blanktarget">www.gutenberg.net</a> <d/iv></pre> End file processing $ Game 每个游戏都包含两个团队。所以我还有一个模型 - Team。它有game_id,team_id和其他字段,作为赢家和详细分数。 我想在一个页面中显示许多游戏,所以我写TeamParticipate 然后,我想在页面上显示参与每个游戏的每两个命令:

@games = Game.where(some_query).includes(:team_participates)

我为页面上的每个团队提供aditional select查询。有没有办法解决这个问题,并通过3个查询加载页面:

-@games.each do |game|
  =game.date
  -game.team_participates.each do |tp|
    =tp.team.title

1 个答案:

答案 0 :(得分:1)

如果您已正确完成关联,

Game.includes(team_participates: :team).where(.....)将会这样做。