我有三款游戏>团队>玩家和我希望能够提交以下内容,以便与这些团队中的多个团队和玩家一起添加游戏。
{"game"=>{"name"=>"championship", "teams_attributes"=>[
{"result"=>"won", "players_attributes"=>{"name"=>"Bob"}},
{"result"=>"lost", "players_attributes"=>{"name"=>"Tad"}}]}}
以下是我的模特:
class Game < ActiveRecord::Base
attr_accessible :name, :teams_attributes, :players_attributes
# Associations
has_many :teams, :inverse_of => :game
has_many :players, :through => :teams
accepts_nested_attributes_for :teams
accepts_nested_attributes_for :players
end
class Team < ActiveRecord::Base
attr_accessible :game_id, :result, :players_attributes
# Associations
belongs_to :game, :inverse_of => :teams
has_many :players, :inverse_of => :team
accepts_nested_attributes_for :players
end
class Player < ActiveRecord::Base
attr_accessible :team_id, :name
# Associations
belongs_to :team, :inverse_of => :players
# belongs_to :game, :through => :team (causes error, doesn't fix)
end
我可以在添加游戏时添加两个团队,但我无法添加游戏,在每个团队中添加两个团队和玩家。我的设置有问题吗?尝试添加时,我不断收到“无法将字符串转换为整数”错误。这是我刚刚玩游戏时遇到的错误&gt;团队,但是当我添加了inverse_of的东西时修复了。
谢谢!
答案 0 :(得分:1)
想出来......我的哈希设置存在问题。 正在使用:
{"game"=>{"name"=>"championship", "teams_attributes"=>[
{"result"=>"won", "players_attributes"=>{"name"=>"Bob"}},
{"result"=>"lost", "players_attributes"=>{"name"=>"Tad"}}]}}
但应该是(在players_attributes周围括号):
{"game"=>{"name"=>"championship", "teams_attributes"=>[
{"result"=>"won", "players_attributes"=>[{"name"=>"Bob"}]},
{"result"=>"lost", "players_attributes"=>[{"name"=>"Tad"}]}]}}