I'm trying to add a player to someone's personal team but I feel like
it's not working because of the table design
ob264 is somewhat right:
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :teams
has_many :players, through: :teams
end
#app/models/team.rb
Class Team < ActiveRecord::Base
#fields id | user_id | other | team | details | created_at | updated_at
belongs_to :user
has_many :player_teams
has_many :players, through: :player_teams
end
#app/models/player.rb
Class Player < ActiveRecord::Base
#fields id | team_id | other | team | details | created_at | updated_at
has_many :player_teams
has_many :teams, through: :player_teams
end
#app/models/player_team.rb
Class PlayerTeam < ActiveRecord::Base
belongs_to :team
belongs_to :player
end
This will allow you to create the following setup:
#config/routes.rb
resources :teams #-> domain.com/teams/5/edit
#app/controllers/players_controller.rb
Class TeamsController < ApplicationController
def edit
@team = Team.find params[:id]
@team.player_teams.build
end
def update
@team = Team.find params[:id]
@team.update(team_params)
end
private
def team_params
params.require(:team).permit(:team, :params, player_teams_attributes: [:player_id, :team_id])
end
end
Although this will need tweaking, it will basically give you the ability to do this:
#app/views/teams/edit.html.erb
<%= form_for @team do |f| %>
<%= f.fields_for :player_teams do |pt| %>
<%= pf.collection_select :player_id, Player.all, :id, :name %> #-> adds a player to your team
<% end %>
<%= f.submit %>
<% end %>
--
ActiveRecord Associations
To answer your question more deeply, you have to appreciate the role of ActiveRecord associations that Rails uses. ActiveRecord is an "ORM" (Object Relational Mapper) layer for data-driven applications, allowing the application-layer to build associated objects together.
The difference here is that if you want to create "relations" / "associations" in your dataset, you have to do several important things:
- You need to appreciate the role of
foreign_keys in your data tables
- You need to associate your models correctly
- You need to work at object level
Firstly, you need to appreciate that ActiveRecord is not magic - it build objects based off the foreign_key in your data tables. This is typically a reference to the primary_key of the parent object:

This means that every time you build an association in Rails, you need to back it up with the data table foreign_keys it needs to find the respective data.
Secondly, you need to then ensure your models are associated correctly. The examples provided are a sufficient demonstration of how this should work (simple once you get your head around it!)
Thirdly, you need to the appreciate working at object level. This is very important - Rails (as it's built on Ruby) is an object orientated framework, meaning that everything you do is based around objects (which are constructed through the Models, from the database)
I am saying this because the way I built the methodology for you above is object orientated -- you're wanting to update a user's "Team" object by adding / removing players from the association. This is a very important pattern to realize within Rails - it will help you profusely if you appreciate how it works