0

これには別の目が必要です。2 人のプレイヤー間の試合を作成すると、Tournament.players は空の配列を返します。

コード

class Tournament < ActiveRecord::Base
  has_many :player_matches
  has_many :matches
  has_many :players, :through => :player_matches  
end

class PlayerMatch < ActiveRecord::Base
  belongs_to :player
  belongs_to :match
  belongs_to :tournament
end

class Player < ActiveRecord::Base
  has_many :player_matches
  has_many :matches, :through => :player_matches
  has_many :tournaments, :through => :player_matches
end
4

1 に答える 1

2

:through二重関係が必要です。

player_matchesずっと。matches_ players_player_matches

class Tournament < ActiveRecord::Base
  has_many :matches
  has_many :player_matches, :through => :matches
  has_many :players, :through => :player_matches  
end

class PlayerMatch < ActiveRecord::Base
  belongs_to :player
  belongs_to :match
end

class Player < ActiveRecord::Base
  has_many :player_matches
  has_many :matches, :through => :player_matches
  has_many :tournaments, :through => :player_matches
end

class Match < ActiveRecord::Base
  belongs_to :tournament  
  has_many :player_matches
  has_many :players, :through => :player_matches
end
于 2013-04-07T19:42:29.303 に答える