Jorge Cuevas Jorge Cuevas - 11 months ago 97
Ruby Question

SystemStackError (stack level too deep) when deleting a record

I'm getting a Stack Overflow error when deleting a record:

SystemStackError (stack level too deep):
app/controllers/orders_controller.rb:18:in `destroy'

I found another System Stack Error related to ruby, but I did not completly get how to fix it since my problem is slightly different.

It is here: Ruby, stack level too deep (SystemStackError)

This is the method, and the second line is causing the error:

def destroy
@order = Order.find(params[:id]).destroy[:success] = true[:success_title] = "¬°Orden eliminada!"[:succes_message] = "La orden #{} fue eliminada."
respond_to do |format|
format.html {redirect_to orders_path}
format.js {}

What can I change to fix this issue?

Answer Source

The stack level too deep error occurs when you try to delete a record with relationships that declare dependent: :destroy linking back to each other.


class Order < ActiveRecord::Base
  has_many :items, dependent: :destroy

class Item < ActiveRecord::Base
  belongs_to :order, dependent: :destroy

Removing dependent: :destroy from the Item assocation should resolve the circular error/stack level too deep.