[英]Rails has_many through seems to not work
我需要第二眼。 當我在兩個玩家之間創建比賽時,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
您需要具有double :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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.