Rails 3 在一个查询中包含多个表
我正在开发一个用于锦标赛的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那应该可以解决问题。实际上,数组表示法和哈希表示法的组合一开始排序有点混乱,但是使用控制台,您会发现哪种语法不会崩溃:)
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 :)