我有一個has_many:through關聯。球員有很多球隊,球隊有很多球員。加盟模式Affiliation屬於玩家和團隊,並且還具有一個year
屬性來跟蹤玩家每年的團隊隸屬關係(或就業)。爲rails 3創建的不正確的數據庫記錄has_many:通過關聯
我似乎無法找出建立基於以下規則具有關聯關係的正確方法:
- 創建一個新的球員。
- 將可能是新的或現有的團隊關聯起來。所以找到它或創建它,但只有在玩家被保存時才創建它。
- 該協會可能會也可能不會包含一年,但只有在保存球員和球隊時才應創建該協會。
玩家模型是這樣的:
class Player < ActiveRecord::Base
attr_accessible :name
has_many :affiliations, :dependent => :destroy
has_many :teams, :through => :affiliations
end
組隊模式是這樣的:
class Team < ActiveRecord::Base
attr_accessible :city
has_many :affiliations, :dependent => :destroy
has_many :players, :through => :affiliations
end
隸屬關係模型是這樣的:
class Affiliation < ActiveRecord::Base
attr_accessible :player_id, :team_id, :year
belongs_to :player
belongs_to :team
end
我已經成功地創建沒有連接模型屬性的關聯記錄
{"player"=>{"name"=>"George Baker", "teams"=>[{"city"=>"Buffalo"}, {"city"=>"Detroit"}]}}
數據庫看起來像:
class PlayersController < ApplicationController
def create
@player = Player.new(params[:player].except(:teams))
unless params[:player][:teams].blank?
params[:player][:teams].each do |team|
team_to_associate = Team.find_or_initialize_by_id(team[:id], team.except(:year)
@player.teams << team_to_associate
end
end
@player.save
respond_with @player
end
end
使用PARAMS像兩隊創建一個新的播放器後:唱的,看起來像PlayersController創建行動
球員
個id:1,名稱:George Baker
隊
ID:1,城市:布法羅
ID:2,城市:西雅圖
隸屬關係
ID:1,player_id:1, team_id:1,year:null
id:2,player_id: 1,team_id:2,year:null
當我試圖介紹這一年時,事情就會崩潰。我最近在PlayersController創建操作的嘗試看起來像:
class PlayersController < ApplicationController
def create
@player = Player.new(params[:player].except(:teams))
unless params[:player][:teams].blank?
params[:player][:teams].each do |team|
team_to_associate = Team.find_or_initialize_by_id(team[:id], team.except(:year)
// only additional line...
team_to_associate.affiliations.build({:year => team[:year]})
@player.teams << team_to_associate
end
end
@player.save
respond_with @player
end
end
現在,使用PARAMS像兩隊創建一個新的播放器時:
{"player"=>{"name"=>"Bill Johnson", "teams"=>[{"id"=>"1"}, {"city"=>"Detroit", "year"=>"1999"}]}}
數據庫的樣子:
玩家
ID:1,名稱:喬治貝克
ID:2,名稱:比爾·約翰遜
隊
ID:1,城市:布法羅
ID:2,城市:西雅圖
ID:3,城市:底特律
從屬關係
ID:1,player_id:1,TEAM_ID:1,年:零
ID:2,player_id:1,TEAM_ID:2年:零
ID:3,player_id:2 ,TEAM_ID:1,年:空
ID:4,player_id:空,TEAM_ID:3,年:1999
ID:5,player_id:2,TEAM_ID:3,年:空
因此,只有兩個應該創建的三個記錄。聯盟記錄ID:3是正確的。對於id:4,player_id丟失。而對於id:5,這一年不見了。
顯然這是不正確的。我哪裏錯了?
感謝
我試過了,它確實有效。所以謝謝!但我很好奇,有沒有一種方法可以建立關聯,以便「@ group.save」保存所有內容? – glevine
它取決於,我不知道@group是什麼,我只是複製/粘貼它從你的答案 –
對不起,這是一個錯誤。我從另一個例子中複製了代碼,並且在那裏留下了@ @組。我編輯了我的問題,所以'@ group'現在是'@ player'。希望澄清它。 – glevine