Я работаю над приложением рельсов для турниров.У меня есть три модели, с которыми я работаю над этим запросом:
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"
В действии show tournaments_controller я вызываю следующий запрос:
Tournament.where(:id => params[:id]).includes(:player_matches, :players).first()
Пока проводятся турниры и player_matchesв одном соединении игроки запрашиваются индивидуально, так как мой код зависит от них:
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
Как я могу изменить это, чтобы все это вытягивалось в одном запросе?