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
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. > > 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, > MarkJust 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)
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.[...]> > 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) >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
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.
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