2013-04-07 1 views
0

두 번째 눈이 필요합니다. 두 플레이어 간의 경기를 만들면 Tournament.players는 빈 배열을 반환합니다.레일을 통해 has_many가 작동하지 않는 것 같습니다.

코드 당신은 이중 :through 관계를 가질 필요가

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 
+0

'Match' 모델을 추가 할 수 있습니까? – Zippie

+0

예, 우리는'Match' 모델을보아야합니다 – OneChillDude

+0

'Tournaments'와'Player'의 많은 플레이어가 일치하는 다형성을 고려할 수도 있습니다. – OneChillDude

답변

1

: player_matches을 통해 matchesplayers을 통해

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