Pretty new to Rails and development in general so sorry if I'm phrasing this question wrong.
I'm creating an app that generates lineups automatically for a kickball/little league team. Users can create a team, and then the Team can create a Game. Each game has_many :innings. When the game is created, it should add a specified amount of innings to that game.
This is where I encounter the problem. Code is below. Say I make a 5-inning game for a Team. It is given the id of 1 and works fine and adds the proper innings. However, if I then delete that game for whatever reason, and make a new one, it creates a new Game that also has the id of 1 - however this time it has 10 innings. Do it once more and you get a 15-inning game and so on. The server seems to be saving and deleting the games just fine, but shouldn't the game ids just keep going up regardless of what I delete? I haven't changed anything regarding auto-incrementing.
Here is my controller:
class GamesController < ApplicationController
before_action :set_team
# GET /games
# GET /games.json
def index
@games = Game.all
end
# GET /games/1
# GET /games/1.json
def show
@game = Game.find(params[:id])
@team = @game.team
end
# GET /games/new
def new
@game = @team.games.build
@game.no_of_innings = nil
end
# GET /games/1/edit
def edit
end
# POST /games
# POST /games.json
def create
@game = @team.games.build(game_params)
@game.user = current_user
respond_to do |format|
if @game.save
(@game.no_of_innings).times do
@game.innings.build
@game.save
end
format.html { redirect_to @game, notice: 'Game was successfully created.' }
format.json { render :show, status: :created, location: @game }
else
format.html { render :new }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /games/1
# PATCH/PUT /games/1.json
def update
respond_to do |format|
if @game.update(game_params)
format.html { redirect_to @game, notice: 'Game was successfully updated.' }
format.json { render :show, status: :ok, location: @game }
else
format.html { render :edit }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end
# DELETE /games/1
# DELETE /games/1.json
def destroy
@game.destroy
respond_to do |format|
format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' }
format.json { head :no_content }
end
end
def generate_lineup
@game = Game.find(params[:id])
@team = @game.team
@game.generate_lineup(@team)
render :show
end
private
# Use callbacks to share common setup or constraints between actions.
def set_team
@team = Team.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def game_params
params.require(:game).permit(:no_of_innings, :opponent, :date)
end
def inning_params
params.require(:inning).permit(:p, :c, :first, :third, :lr, :rr, :l, :lc, :rc, :r, :bench)
end
end
And my Game model:
class Game < ApplicationRecord
has_many :innings
belongs_to :user
belongs_to :team
def generate_lineup(t)
clear_all
roster = get_working_roster(t)
roster = create_bench_order(roster)
set_defense(self, roster)
end
#get working roster of people
def get_working_roster(t)
Player.all.select { |p| (p.team_id == team.id) && (p.active == true) }
end
def clear_all
self.innings.each do |inning|
inning.p = nil
inning.c = nil
inning.first = nil
inning.third = nil
inning.lr = nil
inning.rr = nil
inning.l = nil
inning.lc = nil
inning.rc = nil
inning.r = nil
inning.bench = nil
end
end
def create_bench_order(players)
kicking_order = []
guys = []
girls = []
players = players.shuffle
players.each do |player|
if player.gender == 'female'
girls << player
else
guys << player
end
end
if guys.length > girls.length
bigger = guys
smaller = girls
elsif girls.length > guys.length
bigger = girls
smaller = guys
elsif guys.length == girls.length
kicking_order = guys.zip(girls).compact.flatten
return kicking_order
end
(smaller.length).times do |x|
kicking_order << bigger[0]
bigger.shift
kicking_order << smaller[0]
smaller.shift
end
index = 0
while bigger.any?
kicking_order.insert(index, bigger[0])
bigger.shift
index += 3
end
return kicking_order
end
def set_defense(game, players)
game.innings.each do |inning|
bench = []
bench_no = players.length - 10
bench_no.times do
player = players[0]
bench << player.name
players.shift
players << player
end
bench_display = ""
bench.each do |x|
bench_display += x + ", "
end
inning.bench = bench_display
playing = players[0...-(bench_no)]
playing = playing.shuffle
playing.each do |plr|
player_prefs = [plr.p1, plr.p2, plr.p3, plr.p4, plr.p5, plr.p6, plr.p7, plr.p8, plr.p9, plr.p10]
index = 0
until index > 9 do
if free?(inning.p) && player_prefs[index] == 'p'
inning.p = plr.name
break
elsif free?(inning.c) && player_prefs[index] == 'c'
inning.c = plr.name
break
elsif free?(inning.first) && player_prefs[index] == 'first'
inning.first = plr.name
break
elsif free?(inning.third) && player_prefs[index] == 'third'
inning.third = plr.name
break
elsif free?(inning.lr) && player_prefs[index] == 'lr'
inning.lr = plr.name
break
elsif free?(inning.rr) && player_prefs[index] == 'rr'
inning.rr = plr.name
break
elsif free?(inning.l) && player_prefs[index] == 'l'
inning.l = plr.name
break
elsif free?(inning.lc) && player_prefs[index] == 'lc'
inning.lc = plr.name
break
elsif free?(inning.rc) && player_prefs[index] == 'rc'
inning.rc = plr.name
break
elsif free?(inning.r) && player_prefs[index] == 'r'
inning.r = plr.name
break
else
index += 1
end
end
end
end
end
def free?(position)
position == nil
end
end
Sorry for any breaches of etiquette here, longtime reader first time poster!