Can't get accepts_nested_attributes_for to work for two levels deep?
Asked Answered
S

1

6

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!

Stopoff answered 19/8, 2013 at 9:5 Comment(5)
Can you provide your controller code and your view code as well. And probably the concrete error, including line number and whole error message?Sahara
Oh, I saw that too late, but there is no belongs_to through association in Rails!Sahara
Hi! My controller is super basic, just calls Game.create(...)Stopoff
as for the belongs_to through, I thought that would fix my error, but it didn't because just what you said. I keep getting an error even with this commented out. I can add a Game with one or more Teams, but I cannot add a Game, with one or more Teams, each with one or more players via the same Game.create().Stopoff
Figured it out... was an issue with my hash setup. Was using: {"game"=>{"name"=>"championship", "teams_attributes"=>[ {"result"=>"won", "players_attributes"=>{"name"=>"Bob"}}, {"result"=>"lost", "players_attributes"=>{"name"=>"Tad"}}]}} But should be: {"game"=>{"name"=>"championship", "teams_attributes"=>[ {"result"=>"won", "players_attributes"=>[{"name"=>"Bob"}]}, {"result"=>"lost", "players_attributes"=>[{"name"=>"Tad"}]}]}}Stopoff
S
1

Figured it out... was an issue with my hash setup. Was using:

{ 
  "game" => {
    "name" => "championship", 
    "teams_attributes" => [
      {"result" => "won", "players_attributes" => {"name" => "Bob"}},
      {"result" => "lost", "players_attributes" => {"name" => "Tad"}}
    ]
  }
}

But should be (brackets around the value of players_attributes]:

{ 
  "game" => {
    "name" => "championship", 
    "teams_attributes" => [
      {"result" => "won", "players_attributes" => [{"name" => "Bob"}]},
      {"result" => "lost", "players_attributes" => [{"name" => "Tad"}]}
    ]
  }
}
Stopoff answered 19/8, 2013 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.