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
On May 19, 7:46 am, inkling <[email protected]> wrote:
> 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.
>
> On May 18, 6:21 pm, brianp <[email protected]> wrote:
>
>
>
> > 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 = �[email protected](params[:user])
> >         @user.roles << Role.find(3)
> >         @user.save!
>
> >         @dealer.addresses.create(params[:mailing_address])
> >         @dealer.addresses.create(params[:billing_address])
> >       end
>
> > cheers,
> > brianp
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to