# Validation on association create

**URL:** <https://www.ruby-forum.com/t/validation-on-association-create/188827>\
**Category:** Rails\
**Created:** [May 19, 2010, 1:23am UTC](https://www.ruby-forum.com/t/validation-on-association-create/188827 "2010-05-19T01:23:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 19, 2010, 1:23am UTC](https://www.ruby-forum.com/t/validation-on-association-create/188827/1 "2010-05-19T01:23:12Z")

</div>

Hey

I’m having the worst time with 1 block of my app lol.

I have an address model with many validations.

I run this code on a 4 model form. If the params for the adresses are  
blank (not valid) or just not valid the objects should not be created  
and neither should the user or dealer. But both the user and dealer  
are being saved and the addresses are throwing no validation errors. I  
know the model is valid as i’ve unit tested it thoroughly. Why are the  
validations not being processed on create. I’ve read the differences  
for new-vs-create-vs-build but it sounds like build associations  
should run validations.

```
  ActiveRecord::Base.transaction do

    params[:mailing_address] ||= []
    params[:billing_address] ||= []

    @dealer = Dealer.new(params[:dealer])
    @dealer.save!

    @user = @dealer.users.new(params[:user])
    @user.roles << Role.find(3)
    @user.save!

    @dealer.addresses.create(params[:mailing_address])
    @dealer.addresses.create(params[:billing_address])
  end

```

cheers,  
brianp

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 19, 2010, 7:47am UTC](https://www.ruby-forum.com/t/validation-on-association-create/188827/2 "2010-05-19T07:47:49Z")

</div>

Can you supply the Dealer and User model validations so we can see  
what they are doing?

When you say “If the params for the adresses are  
blank (not valid) or just not valid the objects should not be created  
and neither should the user or dealer” then this wouldn’t even work:

@dealer = Dealer.new(params[:dealer])  
@dealer.save!

Since you are asking a dealer record to save but an address hasn’t  
been added to it yet. That should bomb out regardless of  
params[:mailing\_address] being blank or not with what you said and  
what you have written here.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [May 19, 2010, 9:55am UTC](https://www.ruby-forum.com/t/validation-on-association-create/188827/3 "2010-05-19T09:55:58Z")

</div>

You need to add a bang to the create methods for the addresses so that  
an exception is raised. Rollbacks only happen if an exception is  
raised.  
ActiveRecord::Base.transaction do  
# (…)  
@dealer.addresses.create!(params[:mailing\_address])  
@dealer.addresses.create!(params[:billing\_address])  
end

Or you could wait with saving the dealer until all it’s children have  
been initiated
