Rails reusing ids when building associated objects?

Rails reusing ids when building associated objects?

Pretty new to Rails and development in general so sorry if I'm phrasing this question wrong.

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) &amp;&amp; (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 &lt;&lt; player
    else
      guys &lt;&lt; player
    end
  end

  if guys.length &gt; girls.length
    bigger = guys
    smaller = girls
  elsif girls.length &gt; 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 &lt;&lt; bigger[0]
    bigger.shift
    kicking_order &lt;&lt; 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 &lt;&lt; player.name
         players.shift
         players &lt;&lt; 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 &gt; 9 do 
         if free?(inning.p) &amp;&amp; player_prefs[index] == 'p'
          inning.p = plr.name
          break
         elsif free?(inning.c) &amp;&amp; player_prefs[index] == 'c'
          inning.c = plr.name
          break
         elsif free?(inning.first) &amp;&amp; player_prefs[index] == 'first'
          inning.first = plr.name
          break
         elsif free?(inning.third) &amp;&amp; player_prefs[index] == 'third'
          inning.third = plr.name
          break
         elsif free?(inning.lr) &amp;&amp; player_prefs[index] == 'lr'
          inning.lr = plr.name
          break
         elsif free?(inning.rr) &amp;&amp; player_prefs[index] == 'rr'
          inning.rr = plr.name
          break
         elsif free?(inning.l) &amp;&amp; player_prefs[index] == 'l'
          inning.l = plr.name
          break
         elsif free?(inning.lc) &amp;&amp; player_prefs[index] == 'lc'
          inning.lc = plr.name
          break
         elsif free?(inning.rc) &amp;&amp; player_prefs[index] == 'rc'
          inning.rc = plr.name
          break
         elsif free?(inning.r) &amp;&amp; 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!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Ruby on Rails vs PHP

Understanding the pros and cons of Ruby on Rails versus PHP is important when deciding how to create your business-critical applications.