Rails 3 在一个查询中包含多个表

发布于 2024-10-29 10:12:54 字数 1807 浏览 1 评论 0原文

我正在开发一个用于锦标赛的 Rails 应用程序。我在这个查询上使用了三个模型:

class Player < ActiveRecord::Base
  validates :name, :uniqueness => true
  has_and_belongs_to_many :tournaments

class Tournament < ActiveRecord::Base
  belongs_to :tournament_type
  has_and_belongs_to_many :players
  has_many :player_matches, :dependent => :destroy

class PlayerMatch < ActiveRecord::Base
  belongs_to :player_one, :class_name => "Player", :foreign_key => "player_one"
  belongs_to :player_two, :class_name => "Player", :foreign_key => "player_two"

在锦标赛控制器的显示操作中,我调用以下查询:

Tournament.where(:id => params[:id]).includes(:player_matches, :players).first()

虽然锦标赛和玩家匹配是在单个连接中完成的,但玩家是单独查询的,因为我的代码依赖于他们:

Player Load (0.4ms)  SELECT `players`.*, t0.tournament_id as the_parent_record_id FROM `players` INNER JOIN `players_tournaments` t0 ON `players`.id = t0.player_id WHERE (t0.tournament_id = 14)
Player Load (0.2ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 5 LIMIT 1
Player Load (0.2ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 9 LIMIT 1
Player Load (0.2ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 1 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 8 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 3 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 2 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 7 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 6 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 4 LIMIT 1

我怎样才能改变这一点,以便将其全部提取到一个查询中?

Im working on a rails application for tournaments. I have three models im working with on this query:

class Player < ActiveRecord::Base
  validates :name, :uniqueness => true
  has_and_belongs_to_many :tournaments

class Tournament < ActiveRecord::Base
  belongs_to :tournament_type
  has_and_belongs_to_many :players
  has_many :player_matches, :dependent => :destroy

class PlayerMatch < ActiveRecord::Base
  belongs_to :player_one, :class_name => "Player", :foreign_key => "player_one"
  belongs_to :player_two, :class_name => "Player", :foreign_key => "player_two"

In the show action of the tournaments_controller, im calling the following query:

Tournament.where(:id => params[:id]).includes(:player_matches, :players).first()

While the tournaments and player_matches are done in a single join, the players are queried individually, since my code depends on them:

Player Load (0.4ms)  SELECT `players`.*, t0.tournament_id as the_parent_record_id FROM `players` INNER JOIN `players_tournaments` t0 ON `players`.id = t0.player_id WHERE (t0.tournament_id = 14)
Player Load (0.2ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 5 LIMIT 1
Player Load (0.2ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 9 LIMIT 1
Player Load (0.2ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 1 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 8 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 3 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 2 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 7 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 6 LIMIT 1
Player Load (0.1ms)  SELECT `players`.* FROM `players` WHERE `players`.`id` = 4 LIMIT 1

How can i change this up, so that it is all pulled in one query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2024-11-05 10:12:54
Tournament.where(:id => params[:id]).includes([{:player_matches => :player_one}, :players]).first()

那应该可以解决问题。实际上,数组表示法和哈希表示法的组合一开始排序有点混乱,但是使用控制台,您会发现哪种语法不会崩溃:)

Tournament.where(:id => params[:id]).includes([{:player_matches => :player_one}, :players]).first()

That should do the trick. And really, the combination of array notation and hash notation is a bit messy to get sorted at first, but work with the console and you'll find out which syntax doesn't crash :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文