# stack level too deep

**URL:** <https://discuss.rubyonrails.org/t/stack-level-too-deep/58109>\
**Category:** rubyonrails-talk\
**Created:** [March 26, 2011, 10:05pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109 "2011-03-26T22:05:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philipp\_Fehre](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@Philipp\_Fehre](https://discuss.rubyonrails.org/u/Philipp_Fehre)\
**Post date:** [March 26, 2011, 10:05pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109/1 "2011-03-26T22:05:17Z")

</div>

Stack Level too deep means there is some function recursing, probably forever. Looking over your code real quick I've seen the :has\_many organizations which probably leads to the recursion. When trying to get the parents, Rails will get the Parents of the Parents and so on since it is a property of the Organization so I guess it is not lazy evaluated. Maybe that helps...

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 26, 2011, 11:10pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109/2 "2011-03-26T23:10:37Z")

</div>

Philipp Fehre wrote in post #989443:

> Stack Level too deep means there is some function recursing, probably forever. Looking over your code real quick I've seen the :has\_many organizations which probably leads to the recursion. When trying to get the parents, Rails will get the Parents of the Parents and so on since it is a property of the Organization so I guess it is not lazy evaluated. Maybe that helps...

Thanks Philipp.. I commented the has\_many :organizations sentence but it still keeps showing the errors... The issue in my code comes from this place

&nbsp;&nbsp;&nbsp;def self.parents

> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@organizations = Organization.where("is\_company = ?",true)

I guess it keeps asking itself about the companies which are companies within the same table... What do you think?

Leo

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [March 26, 2011, 11:34pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109/3 "2011-03-26T23:34:02Z")

</div>

yep... and "self" is one of those companies, so it keeps looping...

Add a condition to exclude the current record:

```
  @organizations = Organization\.where\(\[&quot;is\_company = ? AND id &lt;&gt;

```

?",true, self.id])

(or whatever it takes - I'm not into Rails 3 yet... too much old 2.x stuff to do!)

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [March 26, 2011, 11:38pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109/4 "2011-03-26T23:38:08Z")

</div>

Ignore me... I see that's a class method, so won't have an instance id...

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [March 26, 2011, 11:43pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109/5 "2011-03-26T23:43:19Z")

</div>

I assume that the "parents" method returns all the Organizations which have child organisations?

If so, you're using this method instead of a scope? Can't you do something along the lines of the pseudo-SQL below:

SELECT \* FROM organisations WHERE organisations.id IN (SELECT parent\_id FROM organisations GROUP BY parent\_id)

If you can get it working in SQL, you can turn it into a scope.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 4, 2011, 11:53pm UTC](https://discuss.rubyonrails.org/t/stack-level-too-deep/58109/6 "2011-04-04T23:53:03Z")

</div>

Michael Pavling wrote in post #989454:
