0
我想創建一個匹配表。此匹配表將查找從團隊表中獲取其信息。我無法讓協會工作。Belongs_to太多列ID
class Match < ActiveRecord::Base
#
end
class Team < ActiveRecord::Base
belongs_to :matches, :class_name => "Match", :foreign_key => "hometeam_id"
belongs_to :matches, :class_name => "Match", :foreign_key => "awayteam_id"
end
我配對錶有
# id
# hometeam_id
# awayteam_id
# …
我的團隊表有
# id
# name
# …
我希望能夠做到以下幾點
game = Match.find(:first)
# <Match id: 1, hometeam_id: 64810937, awayteam_id: 78380562,
game.hometeam
# returns "Toronto"
我不太確定我的belongs_to是否正確。我覺得我正在重複自己,他們可能是一個更好的方法。思考?
UPDATE解決
class Match < ActiveRecord::Base
belongs_to :hometeam, :class_name => "Team"
belongs_to :awayteam, :class_name => "Team"
end
class Team < ActiveRecord::Base
has_many :homegames, :class_name => "Match", :foreign_key => "hometeam_id"
has_many :awaygames, :class_name => "Match", :foreign_key => "awayteam_id"
end
離開這件事對誰比誰運行到類似的問題。
試了一下。只是一個錯誤。 – alenm 2011-04-30 01:15:09