2015-10-05 82 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

相关问题