validation on association create

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

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.

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