Home > Software engineering >  500 Internal Server Error and looking through the wrong table when trying to make a DELETE request i
500 Internal Server Error and looking through the wrong table when trying to make a DELETE request i

Time:09-28

I was trying to get some basic CRUD functionality working in my rails app but the DELETE in particular keeps showing up a 500 internal server error.

Started DELETE "/developers/7" for ::1 at 2021-09-27 13:40:00  0530
Processing by DevelopersController#destroy as */*
  Parameters: {"id"=>"7"}
   (0.2ms)  SELECT sqlite_version(*)
  ↳ app/controllers/developers_controller.rb:44:in `set_developer'
  Developer Load (0.4ms)  SELECT "developers".* FROM "developers" WHERE "developers"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
  ↳ app/controllers/developers_controller.rb:44:in `set_developer'
  TRANSACTION (0.1ms)  begin transaction
  ↳ app/controllers/developers_controller.rb:38:in `destroy'
  TRANSACTION (0.1ms)  rollback transaction
  ↳ app/controllers/developers_controller.rb:38:in `destroy'
Completed 500 Internal Server Error in 9ms (ActiveRecord: 1.6ms | Allocations: 2233)


  
ActiveRecord::StatementInvalid (Could not find table 'developers_teams'):
  
app/controllers/developers_controller.rb:38:in `destroy'

The actual model I want it to look into is developer and the get and post requests do this just fine, but it keeps saying it can't find the table developers_teams which is my join table for HABTM association between developer and team.

Relevant models:


team.rb

class Team < ApplicationRecord
    validates :name, presence: true
    has_many :messages
    has_and_belongs_to_many :developers
end

developer.rb

class Developer < ApplicationRecord
    validates :full_name, presence: true
    has_and_belongs_to_many :teams
end

The controller for developers:

class DevelopersController < ApplicationController
  before_action :set_developer, only: [:show, :update, :destroy]

  # GET /developers
  def index
    @developers = Developer.all

    render json: @developers
  end

  # GET /developers/1
  def show
    render json: @developer
  end

  # POST /developers
  def create
    @developer = Developer.new(developer_params)

    if @developer.save
      render json: @developer, status: :created, location: @developer
    else
      render json: @developer.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /developers/1
  def update
    if @developer.update(developer_params)
      render json: @developer
    else
      render json: @developer.errors, status: :unprocessable_entity
    end
  end

  # DELETE /developers/1
  def destroy
    @developer.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_developer
      @developer = Developer.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def developer_params
      params.require(:developer).permit(:full_name, :email, :mobile)
    end
end

Join table schema:

class AddHabtmForTeamsAndDevelopers < ActiveRecord::Migration[6.1]
  def change
    create_table :teams_developers, id: false do |t|
      t.belongs_to :team
      t.belongs_to :developer
    end
  end
end

CodePudding user response:

By default, has_and_belongs_to_many expects a table name with joined model names in alphabetical order, so it should be developers_teams.

You can override the default convention by setting join_table option:

has_and_belongs_to_many :teams, join_table: 'teams_developers'

Now, the reason it at all looks at developers_team table is that rails want to remove all the joining records in the join table referencing the record you're deleting.

  • Related