Tengo los modelos Player
, Team
y PlayerTeam
; PlayerTeam
es la tabla del medio que forma la relación de muchos a muchos entre Player
y Team
.
player.rb:
class Player < ApplicationRecord
has_many :player_teams
has_many :teams , through: :player_teams
before_save do
self.numero_goals = 0
self.reputation = 0
self.plays_win = 0
self.plays_lose = 0
end
end
team.rb:
class Team < ApplicationRecord
has_many :player_teams, dependent: :destroy
has_many :players, through: :player_teams
has_many :team_battles, dependent: :destroy
has_many :battles, through: :team_battles
end
player_team.rb:
class PlayerTeam < ApplicationRecord
belongs_to :player
belongs_to :team
end
Las rutas actualmente lo tengo así:
resources :players do
resources :player_teams
end
resources :teams do
resources :player_teams
end
Quiero saber cómo puedo organizarlo de la mejor manera para que, una vez que se cree un Player
, este pueda escoger un Team
o crearlo, para así guardarle automáticamente en la tabla PlayerTeam
.