簡體   English   中英

Rails has_many通過似乎不起作用

[英]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通過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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM