2015-10-05 34 views
1

我正在嘗試爲比賽,遊戲,團隊建模。Rails使用來自同一模型的兩個外鍵加入表

class Contest < ActiveRecord::Base 
has_many :games 
end 

class Game < ActiveRecord::Base 
belongs_to :contest 
belongs_to :away_team, :class_name => "Team", :foreign_key => :away_team_id 
belongs_to :home_team, :class_name => "Team", :foreign_key => :home_team_id 
end 

class Team < ActiveRecord::Base 
belongs_to :sport 
has_many :away_teams, :class_name => "Game", :foreign_key => :away_team_id 
has_many :home_teams, :class_name => "Game", :foreign_key => :home_team_id 
end 

移民遊戲:

class CreateGames < ActiveRecord::Migration 
def change 
    create_table :games do |t| 
    t.references :contest, index: true, foreign_key: true 
    t.integer :home_team_id, index: true, foreign_key: true 
    t.integer :away_team_id, index: true, foreign_key: true 

    t.timestamps null: false 
    end 
    end 
end 

博弈模型需要引用組隊模式兩次獲得away_team_id和home_team_id。我不認爲這個設置是正確的,因爲當在控制檯中搞亂遊戲對象時,我無法訪問away_team_id和home_team_id變量。

任何指針?

+0

這也被稱爲 「自我指涉加入」 http://railscasts.com/episodes/163-self -referential關聯?視圖=評論 –

回答

0

我不得不取消註釋

permit_params :list, :of, :attributes, :on, :model, :contest_id, :away_team_id, :home_team_id 
在應用程序/管理

/game.rb

相關問題