# Validates\_associated bug?

**URL:** <https://www.ruby-forum.com/t/validates-associated-bug/225915>\
**Category:** Rails\
**Created:** [August 17, 2006, 4:58am UTC](https://www.ruby-forum.com/t/validates-associated-bug/225915 "2006-08-17T04:58:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [August 17, 2006, 4:58am UTC](https://www.ruby-forum.com/t/validates-associated-bug/225915/1 "2006-08-17T04:58:36Z")

</div>

i seem to be experiencing a bug where rails tells me that my  
organization object’s associated credit card is invalid (activerecord  
validation-wise invalid), when i know it to be valid. this happens  
only if i tell the organization to validate\_associated :credit\_card.

i know the associated credit card is valid, because a) that  
credit\_card object itself throws no validation errors, and b) if i  
change @credit\_card.save to  
@credit\_card.update\_attributes(params[:credit\_card]) i get a new  
credit\_card record in the database, yet the overall transaction fails  
later because @organization still thinks the credit card is invalid.

taking the validates\_associated :credit\_card out of Organization lets  
the whole operation complete normally, using the same input data.

i have a model structure roughly like so:

User  
id, etc…

BusinessUser  
id, etc…  
user\_id

Organization  
id, etc…  
primary\_business\_user\_id  
credit\_card\_id

CreditCard  
id, etc…

and a business\_user\_controller to create it all:

def create

@user = User.new(params[:user])  
@business\_user = BusinessUser.new(params[:business\_user])  
@business\_user.user = @user  
@organization = Organization.new(params[:organization])  
@organization.primary\_user = @business\_user  
@credit\_card = CreditCard.new  
credit\_card\_validated = true

has\_credit\_card = params[:has\_credit\_card] == ‘true’  
if has\_credit\_card  
@credit\_card = CreditCard.new(params[:credit\_card])  
credit\_card\_validated = @credit\_card.save  
@organization.credit\_card = @credit\_card  
end

begin  
User.transaction do  
if @user.save! and  
#((has\_credit\_card and @credit\_card.save!) or true) and  
(credit\_card\_validated and true or raise  
ActiveRecord::RecordNotSaved) and  
@organization.save! and  
@business\_user.save!  
flash[:notice] = ‘Account created. Check your email for login  
details.’  
Mailer::deliver\_signup\_confirmation(@user)  
redirect\_to :controller =\> ‘login’, :action =\> ‘login’  
end

```
end # end User.transaction

```

rescue  
render :action =\> ‘new’  
end  
end

can anyone offer some insight with what’s here, or do you want me to  
zip and post the entire working example? i’ve been pulling my hair  
out for a week now poring over debug output from this :-/

thanks,  
jeff

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [August 19, 2006, 5:52am UTC](https://www.ruby-forum.com/t/validates-associated-bug/225915/2 "2006-08-19T05:52:10Z")

</div>

no ideas anyone? do you need more info?

---

<div class="post-metadata">

**Author:** ![jemminger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c0e974/32.png) [@jemminger](https://www.ruby-forum.com/u/jemminger)\
**Post date:** [August 19, 2006, 7:08am UTC](https://www.ruby-forum.com/t/validates-associated-bug/225915/3 "2006-08-19T07:08:17Z")

</div>

On 8/18/06, jeff emminger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > i know the associated credit card is valid, because a) that
> > 
> > credit\_card\_id  
> > @business\_user.user = @user  
> > end  
> > created. Check your email for login details.’  
> > end
> > 
> > can anyone offer some insight with what’s here, or do you want me to  
> > zip and post the entire working example? i’ve been pulling my hair  
> > out for a week now poring over debug output from this :-/

It all comes down to that validates\_associated? Axe it, move on!  
You’ll  
‘aha’ and solve it later.

jeremy
