So I have to following models.

class Team < ActiveRecord::Base belongs_to :user has_and_belongs_to_many :players accepts_nested_attributes_for :players end class Player < ActiveRecord::Base has_many :statistics has_and_belongs_to_many :teams end

I wish to build a team that has players, these will be selected by the user. I can do this perfectly in the console by doing to following.

@user = User.find(10) @team = @user.build_team(name: "MyTeam", points: 0) #<Team team_id: nil, name: "MyTeam", points: 0, user_id: 10> @team.players.build(name: "Messi") #<Player player_id: nil, name: "Messi", role: nil> @team.save

However I am really struggling passing parameters due to strong params. Here is my view

<%= form_for :team do |f| %> <%= f.label :name %><br /> <%= f.text_field :name %> <%= f.fields_for :players do |players| %> <%= players.label :player_name %> <%= players.text_field :name %> <% end %> <div><%= f.submit "Create Team" %></div> <% end %>

I want to build the team using the team parameters and the players using the player parameters, however I cannot figure out how to get this working in the controller.

class TeamController < ApplicationController before_action :authenticate_user! def new end def create @user = User.find(current_user.id) @team = @user.build_team(team_params) #Just the team paramaters @team = @team.players.build(player_params)# I want just the player params @team.save end private # I can add the player param as nested i.e. .permit(:name, :players => [:name]) # but then build_team complains about receiving an array. def team_params params.require(:team).permit(:name) end end

Any solutions welcome, as are any improvements.

EDIT - ADDED SCHEMA

create_table "players", primary_key: "player_id", force: true do |t| t.string "name", limit: 50, null: false t.string "role", limit: 30, null: false end create_table "players_teams", id: false, force: true do |t| t.integer "player_id", null: false t.integer "team_id", null: false end # players_teams is a Composite Primary Key, as instructed in the guides; # also essential for targeting. create_table "teams", primary_key: "team_id", force: true do |t| t.string "name", limit: 200, null: false t.integer "points", default: 0, null: false t.integer "user_id", null: false end

EDIT 2

Since this has not yet been answered I'll add more explanation as to what I am attempting.

The user has one team, I can build the team and the relationship is also built thanks to ActiveRecord. The users team then has many players and players have many teams, when I try to build this relationship the players table never changes, no relationship is created.

I feel I should stress again that the following works perfectly in the rails console

@user = User.find(10) @team = @user.build_team(name: "MyTeam", points: 0) #<Team team_id: nil, name: "MyTeam", points: 0, user_id: 10> @team.players.build(name: "Messi") #<Player player_id: nil, name: "Messi", role: nil> @team.save

Team is set to accept nested parameters so I thought this would work.

@team = @user.build_team(team_params) def team_params params.require(:team).permit(:name, players_attributes: [:name, :role]) end

I believe this should build the players model and create the relationship however no player is ever inserted and no relationship built.