# Stack level too deep error :(

**URL:** <https://www.ruby-forum.com/t/stack-level-too-deep-error/57271>\
**Category:** Rails\
**Created:** [April 8, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/stack-level-too-deep-error/57271 "2006-04-08T22:07:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 8, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/stack-level-too-deep-error/57271/1 "2006-04-08T22:07:50Z")

</div>

Hi,

If anyone has some time to help with my problem that would be really  
good 🙂

I am getting a “stack level too deep” error when saving an object to the  
database when it is has a has\_one relationship to another object I wish  
to save and associate after it.

If goes a little like this

class Quote \< ActiveRecord::Base  
belongs\_to :eventdetail  
has\_many :bookings

# An eventdetail belongs to either a quote or booking

class Eventdetail \< ActiveRecord::Base  
has\_one :quote  
has\_one :booking

# A booking can refer to a previous quote

class Booking \< ActiveRecord::Base  
belongs\_to :eventdetail  
belongs\_to :quote  
end

I have removed a few other things to keep it simple but I don’t think  
the problem relates to those bits. I have created a form for Quote  
which renders a component for a new Eventdetail, to the user the  
eventdetail appears as part of new quote (or booking).

When I go to save the quote I do this:

```
Quote.transaction {
   save_result = @eventdetail.save
   if save_result
     @quote.eventdetail = @eventdetail
     save_result &&= @quote.save
   end
}
if save_result
   flash['notice'] = 'Quote was successfully created.'
   redirect_to(:action => 'show', :id => @quote.id)
else
#quote didn't save, render new, etc
end

```

It all fails after validation on @eventdetail.save with a stack overflow  
error…

Any help would be appreciated 🙂

Cheers,  
Mark

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 9, 2006, 12:06am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-error/57271/2 "2006-04-09T00:06:36Z")

</div>

On Saturday 08 April 2006 22:05, Mark P. wrote:

> class Quote \< ActiveRecord::Base  
> belongs\_to :eventdetail  
> Quote.transaction {  
> #quote didn’t save, render new, etc  
> end
> 
> It all fails after validation on @eventdetail.save with a stack overflow  
> error…
> 
> Any help would be appreciated 🙂
> 
> Cheers,  
> Mark

Just as a quick answer, stack level too deep is usually the case in  
infinite  
recursion or loop. I think you have something going on here like that.  
Perhaps  
something related to the automatic saving of associated objects. I am  
somewhat  
confused at the moment which associations are saved automaticly, but you  
might  
want to look into that.

Anybody with some knowlege about saving associatons, please help (me as  
well 🙂 ). For example, the docs say

“Assigning an object to a belongs\_to association does not save the  
object,  
since the foreign key field belongs on the parent. It does not save the  
parent  
either.”

But I do know from experience that saving the object which says  
belongs\_to also  
saves the associated object. So what is meant by the quote from the doc?  
Are  
they litterly talking about saving on assigning itself, instead of  
cascade-saving?

(footnote: for some reason, associated objects are only saved when the  
association is new, and not when it’s a modified existing record. Why?  
Beats  
me)

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 9, 2006, 10:43am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-error/57271/3 "2006-04-09T10:43:26Z")

</div>

Wiebe C. wrote:

> On Saturday 08 April 2006 22:05, Mark P. wrote:
> 
> > Hi,
> > 
> > If anyone has some time to help with my problem that would be really good 🙂
> > 
> > I am getting a “stack level too deep” error when saving an object to the  
> > database when it is has a has\_one relationship to another object I wish  
> > to save and associate after it.  
> > […]  
> > "Assigning an object to a belongs\_to association does not save the object,  
> > me)

Hi thanks for your reply!

It is basically what I was thinking too, if someone could help explain  
when foreign keys are set and when associated records are saved that  
would be excellent.

Cheers  
Mark

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 9, 2006, 10:55am UTC](https://www.ruby-forum.com/t/stack-level-too-deep-error/57271/4 "2006-04-09T10:55:35Z")

</div>

On Sunday 09 April 2006 10:40, Mark P. wrote:

> It is basically what I was thinking too, if someone could help explain  
> when foreign keys are set and when associated records are saved that  
> would be excellent.

You can easily check that out by starting the scripts/console in your  
project  
dir, and make objects manually (by entering the proper ruby code),  
saving one,  
and seeing if the associated object is saved as well.

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 9, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/stack-level-too-deep-error/57271/5 "2006-04-09T23:43:17Z")

</div>

Wiebe C. wrote:

> On Sunday 09 April 2006 10:40, Mark P. wrote:
> 
> > It is basically what I was thinking too, if someone could help explain  
> > when foreign keys are set and when associated records are saved that  
> > would be excellent.
> 
> You can easily check that out by starting the scripts/console in your project  
> dir, and make objects manually (by entering the proper ruby code), saving one,  
> and seeing if the associated object is saved as well.

Never thought I could do that… excellent have found the problem, fixed  
by creating a polymorphic association between eventdetail =\> { quote,  
booking }

Cheers,  
Mark
