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

GET /games/1 GET /games/1.json

def show
@game = Game.find(params[:id])
@team = @game.team

GET /games/new

def new

@game = @team.games.build
@game.no_of_innings = nil


GET /games/1/edit

def edit

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

    format.html { redirect_to @game, notice: 'Game was successfully created.' }
    format.json { render :show, status: :created, location: @game }
    format.html { render :new }
    format.json { render json: @game.errors, status: :unprocessable_entity }


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 }
format.html { render :edit }
format.json { render json: @game.errors, status: :unprocessable_entity }

DELETE /games/1 DELETE /games/1.json

def destroy
respond_to do |format|
format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' }
format.json { head :no_content }

def generate_lineup
@game = Game.find(params[:id])
@team = @game.team

render :show


# Use callbacks to share common setup or constraints between actions.
def set_team
@team = Team.find(params[:id])

# 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)

def inning_params
  params.require(:inning).permit(:p, :c, :first, :third, :lr, :rr, :l, :lc, :rc, :r, :bench)


And my Game model:

class Game < ApplicationRecord
has_many :innings
belongs_to :user
belongs_to :team

def generate_lineup(t)
   roster = get_working_roster(t)
   roster = create_bench_order(roster)
   set_defense(self, roster)

#get working roster of people
def get_working_roster(t)
    Player.all.select { |p| (p.team_id == team.id) &amp;&amp; (p.active == true) }

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

def create_bench_order(players)
  kicking_order = []
  guys = []
  girls = []
  players = players.shuffle
  players.each do |player|
    if player.gender == 'female'
      girls &lt;&lt; player
      guys &lt;&lt; player

  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

  (smaller.length).times do |x|
    kicking_order &lt;&lt; bigger[0]
    kicking_order &lt;&lt; smaller[0]

  index = 0
  while bigger.any?
    kicking_order.insert(index, bigger[0])
    index += 3
  return kicking_order

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 &lt;&lt; player

     bench_display = ""
     bench.each do |x|
       bench_display += x + ", "
     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
         elsif free?(inning.c) &amp;&amp; player_prefs[index] == 'c'
          inning.c = plr.name
         elsif free?(inning.first) &amp;&amp; player_prefs[index] == 'first'
          inning.first = plr.name
         elsif free?(inning.third) &amp;&amp; player_prefs[index] == 'third'
          inning.third = plr.name
         elsif free?(inning.lr) &amp;&amp; player_prefs[index] == 'lr'
          inning.lr = plr.name
         elsif free?(inning.rr) &amp;&amp; player_prefs[index] == 'rr'
          inning.rr = plr.name
         elsif free?(inning.l) &amp;&amp; player_prefs[index] == 'l'
          inning.l = plr.name
         elsif free?(inning.lc) &amp;&amp; player_prefs[index] == 'lc'
          inning.lc = plr.name
         elsif free?(inning.rc) &amp;&amp; player_prefs[index] == 'rc'
          inning.rc = plr.name
         elsif free?(inning.r) &amp;&amp; player_prefs[index] == 'r'
          inning.r = plr.name
          index += 1

def free?(position)
  position == nil


Sorry for any breaches of etiquette here, longtime reader first time poster!

Ruby vs Ruby on Rails web framework

Rails is a development tool which gives web developers a framework, providing structure for all the code they write. The Rails framework helps developers to build websites and applications. Ruby is a programming language stronger than Perl and more object-oriented than Python. It is being developed with increasing productivity.

Ruby on Rails vs PHP

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.

Originally published at https://www.engineyard.com

There’s more than one way to build a web application. No matter what type of application you are trying to create, your programmers have their preferred approach and their preferred code languages to accomplish the task. In the world of web applications, most program developers have to decide between Ruby on Rails versus PHP.

Ruby on Rails consists of Ruby, which is a scripting language, and Rails, which is a web development framework; PHP is a server-side scripting language. Both programming languages have been around since the mid-1990s, but PHP rules the web, while Ruby on Rails is more popular for business web applications. Understanding the pros and cons of Ruby on Rails versus PHP is important when deciding how to create your business-critical applications.

Ruby on Rails Versus PHP at First Glance

Both Ruby on Rails and PHP are open source, so there are no licensing fees. However, because PHP is used to run most of today’s web systems, there are more PHP programmers than Ruby developers, which means there is a larger pool of PHP experts and a larger open source library to draw from.

Part of the reason PHP is more popular with web developers is because it is easier to learn. PHP is also an object-oriented programming language, which makes it easier to be more creative and tackle tougher software challenges.

Once web developers master PHP, many of them choose to add Ruby on Rails to their expertise because of the advantages and power that Ruby on Rails offers for business application development. Ruby and Rails were created together to deliver web solutions, and the primary difference between PHP and Ruby on Rails is that Rails requires you to understand the full stack, including the web server, application server, and database engine.

Since both Ruby and PHP are open source, the support of the programming communities is an important differentiator. PHP has more deployments so it has a larger developer community, but the Ruby on Rails community is very skilled and enthusiastic and they want to share, so there is a growing library of ready-to-use Ruby gems.

Differences in Deployment

When it comes to deployment, PHP is very easy to implement. You simply transfer files to the web server via FTP and that’s it. With PHP, you don’t need to worry about the web stack. Most hosting services use a combination of open source for the stack, including Linux, Apache, MySQL, and PHP (LAMP), so once the files are loaded, they just run. That’s the advantage of server-side software.

Ruby on Rails is more complex to deploy because you have to know the full stack. That means knowing the details of the web server (e.g., Apache or NginX), as well as the database. You have to go through more steps, such as precompiling assets to make sure all the right files are there. This is the price of being able to design and deploy more complex applications.

Where Ruby on Rails really shines is in the software development process itself. Since Ruby is an object-intensive language, everything is an object, including classes and modules, with Rails providing an integrated test framework. PHP is not always object-oriented, so coding can be laborious and time-consuming. Applications can be built and tested in Ruby on Rails much faster than in PHP, so even if there is some debugging involved, Ruby on Rails dramatically reduces the time to deployment.

As noted above, PHP applications are relatively simple to deploy since there is no stack to worry about, and they are relatively inexpensive to host. Hosting Ruby on Rails applications is another story. Not all hosting providers will support Ruby on Rails, and those that do usually add additional a la carte fees because Ruby applications require more services.

The Business Case for Ruby on Rails versus PHP

While it’s clear that Ruby is a more difficult programming language to master, in many ways, it is a more robust language that is better suited for creating business applications. PHP was created specifically for the web, but Ruby on Rails offers much more.

For one thing, Ruby on Rails applications tend to be cleaner and more compact. Because PHP is so simple, it lends itself to sloppy coding that can be impossible to maintain. Ruby has the advantage of being more elegant and concise, and the documentation for Ruby applications tends to be generated with the code so anyone can make revisions or upgrades.

Most importantly, Ruby on Rails lends itself to agile software practices and rapid application development (RAD). Rails is a mature framework that allows programmers to create maintainable software, and it has integrated testing tools that shorten the developer cycle. When you consider the cost of talented programmers (and you know that time is money), reducing development time can mean substantial savings.

Depending on your business development needs, you may be leaning toward PHP or Ruby on Rails. Each has its strengths and weaknesses, but Ruby on Rails continues to gain popularity for business-critical and e-commerce applications because of its versatility, scalability, and upgradability. In the end, you have to consider which language will deliver a cleaner, more stable application that can evolve and grow with your business.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Learn More

PHP for Beginners - Become a PHP Master - CMS Project

Learn Object Oriented PHP By Building a Complete Website

PHP OOP: Object Oriented Programming for beginners + Project

Laravel PHP Framework Tutorial - Full Course for Beginners (2019)

Symfony PHP Framework - Full Course

What is a Full Stack Developer?

Build RESTful API In Laravel 5.8 Example

Laravel 5.8 Tutorial from Scratch for Beginners

Build a CRUD Operation using PHP & MongoBD

Build a CRUD Web App with Ruby on Rails

Ruby on Rails Tutorial for Beginners

How To Use PostgreSQL with Your Ruby on Rails Application on macOS

Python, Ruby, and Golang: A Command-Line Application Comparison