# Belongs\_to causing endless loop on first call to save!

**URL:** <https://www.ruby-forum.com/t/belongs-to-causing-endless-loop-on-first-call-to-save/143181>\
**Category:** Rails\
**Created:** [July 16, 2008, 2:29am UTC](https://www.ruby-forum.com/t/belongs-to-causing-endless-loop-on-first-call-to-save/143181 "2008-07-16T02:29:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Frith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@Ryan\_Frith](https://www.ruby-forum.com/u/Ryan_Frith)\
**Post date:** [July 16, 2008, 2:29am UTC](https://www.ruby-forum.com/t/belongs-to-causing-endless-loop-on-first-call-to-save/143181/1 "2008-07-16T02:29:19Z")

</div>

Hi,

I have a situation I’m hoping someone out there may be able to shed some  
light on. I have a Rails app (2.1.0 on Ruby 1.8.7) with a wizard-based  
sign  
up process, that has recently been changed from storing incremental data  
in  
the database to having a medium sized object graph living in the user  
session until the user completes the entire sign up process (this is a  
business requirement now). After the user commits all relevant data,  
I’m  
now calling save! on the top-level object to save all the rows at once.  
However, when the call to save! happens–and because I have two objects  
that  
have a belongs\_to association, each pointing to other in addition to the  
normal bidirectional association–I end up in an endless validation  
loop.  
Funny thing is, when I was incrementally saving the data, this issue  
never  
arose–another way to put it, those relationships never caused a problem  
when saving/retrieving already-existing rows.

In the model tier, I have the following classes defined:

class PrimaryAccount \< Account  
has\_many :payment\_methods, :foreign\_key =\> “account\_id” #using single  
table inheritance, which is why foreign key is not “primary\_account\_id”  
belongs\_to :current\_payment\_method, :class\_name =\> “PaymentMethod”,  
:foreign\_key =\> “current\_payment\_method\_id” #points to same table, so I  
can  
have many payment\_methods, but also mark as being the current one to  
bill  
…

class PaymentMethod \< ActiveRecord::Base  
belongs\_to :account #normal bidirectional association  
…

class CreditCard \< PaymentMethod  
…

In the controller tier, I have a session object that–on the final  
step–loads an Account model object based on the current session state,  
and  
then calls save! on that model:

class PrimaryAccountUser \< User

attr\_reader :credit\_card

def initialize(service\_id)  
@credit\_card = {} #hash for storing credit card attributes  
end

def persist\_to\_database  
add\_session\_data\_to\_account(PrimaryAccount.new)  
account.save!  
@id = account.id  
return account  
end

private

def add\_session\_data\_to\_account(account) #adds the impl-specific  
things to  
the account

```
if(!@credit_card.blank?)
  card = CreditCard.new do |cc|
    cc.account_type = @credit_card[:type]
    cc.account_name = @credit_card[:name]
    cc.account_number = @credit_card[:number]
    cc.cvv = @credit_card[:cvv]
    cc.account_expiration = @credit_card[:expiration]
    cc.address = @credit_card[:address]
    cc.city = @credit_card[:city]
    cc.state = @credit_card[:state]
    cc.zip = @credit_card[:zip]
    cc.country = @credit_card[:country]
    cc.phone = @credit_card[:phone]
  end

  #strange...this won't populate the account_id on save!
  account.payment_methods << card

  #again, if I don't do this, then the payment_method won't have a

```

populated account\_id on save!  
card.account = account

```
  #doing this causes the endless validation loop
  account.current_payment_method = card

end

return account

```

end

Am I crazy for thinking this ought to work?

Thanks in advance,

Ryan Frith  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Ryan\_Frith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@Ryan\_Frith](https://www.ruby-forum.com/u/Ryan_Frith)\
**Post date:** [July 16, 2008, 5:17am UTC](https://www.ruby-forum.com/t/belongs-to-causing-endless-loop-on-first-call-to-save/143181/2 "2008-07-16T05:17:16Z")

</div>

It just seems way too overengineered to me. When you add a card to the  
system, just add the card

In the controller, for example:

def create  
card = Card.new(params[:card]  
card.account\_id = session[:user\_id] # or current\_user.id or  
wherever  
you store the id of the currently logged in user.  
if account.save  
…  
else  
…  
end  
end

No need to do all that account \<\< card stuff because the web is  
stateless.  
When it’s saved, you’re going to redirect them, a new request will be  
made  
by your user, and the records get queried again. Plus you save a ton of  
overhead.

just my .02.

Or maybe I’m oversimplifying your situation, but I just like to keep  
things  
as simple as possible.

---

<div class="post-metadata">

**Author:** ![Ryan\_Frith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecb155/32.png) [@Ryan\_Frith](https://www.ruby-forum.com/u/Ryan_Frith)\
**Post date:** [July 16, 2008, 7:08am UTC](https://www.ruby-forum.com/t/belongs-to-causing-endless-loop-on-first-call-to-save/143181/3 "2008-07-16T07:08:56Z")

</div>

Maybe you didn’t see the fine print–I’m saving the entire object  
graph at once; there is no session[:user].id or current\_user. I  
greatly simplified the code for the sake of brevity, but all the  
objects get assembled in the final sign-up step from state stored in  
the session over many screens (wizard) and then all the objects in the  
graph get persisted from the call to the top-level object’s save!  
method. This was a design decision to not save session state to DB in  
between requests for the sign up process. Anyways, the issue may be  
related to this one:

[http://dev.rubyonrails.org/ticket/1796](http://dev.rubyonrails.org/ticket/1796)

Anyone else have any ideas?

Thanks,

Ryan
