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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <brian.o.pea...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 = -vLsUqK4x/yapYMH4hxceFw@public.gmane.org(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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <n...-UgwZ4owrJFB8UrSeD/g0lQ@public.gmane.org> 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 <brian.o.pea...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 = -vLsUqK4x/yapYMH4hxceFw@public.gmane.org(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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.