I have three models Game > Team > Players and I want to be able to submit the following to add a game along with multiple teams and players on those teams.
{"game"=>{"name"=>"championship", "teams_attributes"=>[
{"result"=>"won", "players_attributes"=>{"name"=>"Bob"}},
{"result"=>"lost", "players_attributes"=>{"name"=>"Tad"}}]}}
Here are my models:
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
I can get it to add two teams when I add a game, but I cannot get it to add a game, add two teams and players on each team. Am I doing something wrong with my setup? I keep getting the "can't convert String into Integer" error when trying to add. This is the same error I was getting when I just had Games > Teams, but was fixed when I added the inverse_of stuff.
Thanks!
belongs_to through
association in Rails! – Sahara