Stack level too deep error in Ruby on Rails
Asked Answered
B

9

30

I'm having a stack level too deep error using Ruby 1.8.7 with Rails 3.0.4 and with the rails console I performed the following commands.

leo%>rails console
Loading development environment (Rails 3.0.4)
ruby-1.8.7-head > leo = Organization.find(1)

SystemStackError: stack level too deep
from /app/models/organization.rb:105:in `parents'

Here is the object that is having issues..

class Organization < ActiveRecord::Base

  has_many                  :group_organizations, :dependent =>
:delete_all
  has_many                  :groups, :through => :group_organizations

  has_many                  :orders
  has_many                  :product_contracts

  has_many                  :people
  accepts_nested_attributes_for :people

  has_many                  :addresses
  accepts_nested_attributes_for :addresses

  has_many                  :organizations
  has_many                  :departments
  has_many                  :organization_credits

  has_many                  :documents

  validates_presence_of     :name



    def self.parents
      @organizations = Organization.where("is_company = ?",true)
      #@organization_parents = []
      select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
      @organization_parents = [select_choice]
      for organization in @organizations
        @organization_parents << [organization.name, organization.id]
      end
      return @organization_parents
    end
Bidet answered 26/3, 2011 at 23:16 Comment(4)
On first look, this is not enough info. Which line is 105? Do you have a redefinition of to_s or inspect? If yes, please show it. Stack level too deep is caused by a recursive method that does not end.Aponte
Line 105 on my code is this > @organizations = Organization.where("is_company = ?",true) It looks like it has been recursing itself to the infinity but then I've commented the line has_many :organizations and well, it keeps showing the same error... Any clues about it?Bidet
just for future references: parent and parents are an existing class methods hence the exceptionPivot
HEY ARE YOU FROM 2022 AND FACING THIS ISSUE! Maybe this is for you: github.com/github/graphql-client/issues/277Symer
B
1

I've found the solution to this issue...

I'm using Rails 3 and my class looks like this (and the problematic methods was this too)

class Organization < ActiveRecord::Base
  def self.parents
    @organizations = self.find :all, :conditions => ['is_company = ? ',true]
    select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
    @organization_parents = [select_choice]
    for organization in @organizations
      @organization_parents << [organization.name, organization.id]
    end
    return @organization_parents
  end 
  #...
end

I did have to hack a lot in the code to find out something was wrong with the named_scope on the line

@organizations = self.find :all, :conditions => ['is_company = ? ',true]

So I had to change it to something like this

@organizations = Organization.where("is_company = ?",true)

But it was wrong too.. So I decided to add an scope for this below the class name so the final code looks like this:

class Organization < ActiveRecord::Base
  scope :company, where("is_company = ?",true)

  def self.parents
    @organizations = self.company
    select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
    @organization_parents = [select_choice]
    for organization in @organizations
      @organization_parents << [organization.name, organization.id]
    end
    return @organization_parents
  end 
  #...
end

So using this line with the scope

@organizations = self.company

it worked flawlessly in every part of the code.

I was wondering if the named_scope is deprecated when using class methods or they are not supported from now and throws an error and not a warning before

Thanks for your help Leo

Bidet answered 4/4, 2011 at 23:50 Comment(0)
A
54

This error generally happens when you accidentally recursively changing an attribute. If you have a username attribute in User model, and a virtual attribute named username, that is directly changing the username, you end up calling the virtual, the virtual calls the virtual again and so on.. Therefore, take a look on whether something like that happens somewhere in your code.

Archfiend answered 26/3, 2011 at 23:48 Comment(0)
A
23

The stack level too deep error occurs also, if you want to destroy a record and you have an association with :dependent => :destroy to another model. If the other model has a association with :dependent => :destroy back to this model, the stack level is too deep, too.

Approximate answered 20/11, 2014 at 8:21 Comment(1)
Just confirming, this happened to me, I had belongs_to :model, dependent: destroy and has_many :other_model, dependent: :destroy that caused this error.Stormystorting
H
5

I had a "stack-level too deep" issue too. it was due to recursiveness in one of my functions and had been caused by a typo as you can see from below:

def has_password?(submitted_password)
  encrypt_password == encrypt(submitted_password)
end

private

def encrypt_password
  self.salt = make_salt unless has_password?(password)
  self.encrypted_password = encrypt(password)
end

I realised I had to change the second line to encrypted and it worked. Just checkout for recursion in your code it must be happening somewhere. Unfortunately I can't be of better use since I can't look at all your code files.

Hellenize answered 22/1, 2012 at 11:33 Comment(0)
H
4

I was getting same stack level too deep error & it turns out that the issue was of recurring rendering of a partial.

I happened to call render a_partial in main view and then in the partial, I accidentally called the same partial again.

HTH

Harmonious answered 11/2, 2014 at 6:57 Comment(0)
T
2

As you are not showing all the code, I can only speculate that you have defined inspect or to_s to build a string containing, among other things the parents.

Your current parents method doesn't seem to be doing anything reasonable, as it returns all organisations that are companies, no matter which association you start from. Thus, any company has itself as parent. Attempting to convert it to string will induce an infinite loop to try to show the parents' of the parents' of ...

In any case, the bulk of your parents method should be in a helper, called something like options_for_parents_select, because that's what it seems to be doing? Even then, the first empty choice should be passed as allow_null to select.

The fact that it sets instance variables is a code smell.

Good luck

Tegular answered 27/3, 2011 at 0:6 Comment(0)
B
1

I've found the solution to this issue...

I'm using Rails 3 and my class looks like this (and the problematic methods was this too)

class Organization < ActiveRecord::Base
  def self.parents
    @organizations = self.find :all, :conditions => ['is_company = ? ',true]
    select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
    @organization_parents = [select_choice]
    for organization in @organizations
      @organization_parents << [organization.name, organization.id]
    end
    return @organization_parents
  end 
  #...
end

I did have to hack a lot in the code to find out something was wrong with the named_scope on the line

@organizations = self.find :all, :conditions => ['is_company = ? ',true]

So I had to change it to something like this

@organizations = Organization.where("is_company = ?",true)

But it was wrong too.. So I decided to add an scope for this below the class name so the final code looks like this:

class Organization < ActiveRecord::Base
  scope :company, where("is_company = ?",true)

  def self.parents
    @organizations = self.company
    select_choice = I18n.t("select") + " "+ I18n.t("segments.description")
    @organization_parents = [select_choice]
    for organization in @organizations
      @organization_parents << [organization.name, organization.id]
    end
    return @organization_parents
  end 
  #...
end

So using this line with the scope

@organizations = self.company

it worked flawlessly in every part of the code.

I was wondering if the named_scope is deprecated when using class methods or they are not supported from now and throws an error and not a warning before

Thanks for your help Leo

Bidet answered 4/4, 2011 at 23:50 Comment(0)
F
0

I got this error when incorrectly creating a has_many relationship like this:

has_many :foos, through: foo

So don't put the same model as 'through' or it will loop endlessly.

Formless answered 5/6, 2020 at 21:23 Comment(0)
I
0

To get a stack trace for this error, you can add the following code at the very beginning of application.rb:

module Kernel
 def require_and_print(string)
  puts string
  require_original(string)
 end
 alias_method :require_original, :require
 alias_method :require, :require_and_print
end

In my case, the error pointed to the meta_request gem, and after removing it, the error disappeared.

Impermanent answered 17/6 at 14:49 Comment(0)
M
-1

If you are getting this error it means rails version that you are using in your application is not compatible with Ruby Version.

Solutions you can use to solve this issue.

1) You need to downgrade the ruby version to older version.

2) or you need to upgrade Rails to latest version.

Madalinemadalyn answered 11/11, 2017 at 5:48 Comment(1)
Totally false advice.Dexterdexterity

© 2022 - 2024 — McMap. All rights reserved.