Forum: Ruby on Rails Stack level too deep error :(

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3bdb0e723531e44040fe7131f723ef0b?d=identicon&s=25 Mark Paxton (Guest)
on 2006-04-08 22:07
(Received via mailing list)
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
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-09 00:06
(Received via mailing list)
On Saturday 08 April 2006 22:05, Mark Paxton 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)
3bdb0e723531e44040fe7131f723ef0b?d=identicon&s=25 Mark Paxton (Guest)
on 2006-04-09 10:43
(Received via mailing list)
Wiebe Cazemier wrote:
> On Saturday 08 April 2006 22:05, Mark Paxton 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
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-09 10:55
(Received via mailing list)
On Sunday 09 April 2006 10:40, Mark Paxton 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.
3bdb0e723531e44040fe7131f723ef0b?d=identicon&s=25 Mark Paxton (Guest)
on 2006-04-09 23:43
(Received via mailing list)
Wiebe Cazemier wrote:
> On Sunday 09 April 2006 10:40, Mark Paxton 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
This topic is locked and can not be replied to.