Scott Burton
2005-Jun-22 19:33 UTC
Multiple model saves, 1 controller to display them all(errors)
I have a controller that takes the form data and saves to 2 different models. In those 2 models I have some validation setup. I can get the error_messages_for :model1 to work but not both. Is there another way to do it? Thanks, Scott Burton www.similarthings.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Russ Smith
2005-Jun-22 19:39 UTC
Re: Multiple model saves, 1 controller to display them all(errors)
This did it for me http://wiki.rubyonrails.com/rails/show/UsefulActionPattern On Jun 22, 2005, at 12:33 PM, Scott Burton wrote:> I have a controller that takes the form data and saves to 2 > different models. In those 2 models I have some validation setup. I > can get the error_messages_for :model1 to work but not both. Is > there another way to do it? > > Thanks, > > Scott Burton > www.similarthings.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Scott Burton
2005-Jun-22 20:31 UTC
RE: Multiple model saves, 1 controller to display them all(errors)
I am using the transaction method but it is still saving here is my code: In my controller I have def signup if @request.post? @site = Site.new(@params[''site'']) @user = User.new(@params[''user'']) begin @user.transaction do @user.save @site.save raise ActiveRecord::RecordInvalid unless @user.errors.empty? && @site.errors.empty? end #methods that do other stuff.... rescue ActiveRecord::RecordInvalid end else @user = User.new end end much like what the example has on that page. I get the errors for each model but.. if I put in the correct user info and it validates but I don''t put in the correct site info and it fails, it still creates that user in the db. _____ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Russ Smith Sent: Wednesday, June 22, 2005 12:39 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Multiple model saves,1 controller to display them all(errors) This did it for me http://wiki.rubyonrails.com/rails/show/UsefulActionPattern On Jun 22, 2005, at 12:33 PM, Scott Burton wrote: I have a controller that takes the form data and saves to 2 different models. In those 2 models I have some validation setup. I can get the error_messages_for :model1 to work but not both. Is there another way to do it? Thanks, Scott Burton www.similarthings.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Russ Smith
2005-Jun-22 20:35 UTC
Re: Multiple model saves, 1 controller to display them all(errors)
Make sure that if your using MySQL that the tables being affected are InnoDB or something else that supports transactions. On Jun 22, 2005, at 1:31 PM, Scott Burton wrote:> I am using the transaction method but it is still saving here is my > code: > > In my controller I have > > def signup > if @request.post? > @site = Site.new(@params[''site'']) > @user = User.new(@params[''user'']) > begin > @user.transaction do > @user.save > @site.save > raise ActiveRecord::RecordInvalid unless > @user.errors.empty? && @site.errors.empty? > end > #methods that do other stuff.... > rescue ActiveRecord::RecordInvalid > end > else > @user = User.new > end > end > > much like what the example has on that page. I get the errors for > each model but.. if I put in the correct user info and it validates > but I don''t put in the correct site info and it fails, it still > creates that user in the db. > > > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Russ Smith > Sent: Wednesday, June 22, 2005 12:39 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Multiple model saves,1 controller to display > them all(errors) > > This did it for me > > http://wiki.rubyonrails.com/rails/show/UsefulActionPattern > > On Jun 22, 2005, at 12:33 PM, Scott Burton wrote: > >> I have a controller that takes the form data and saves to 2 >> different models. In those 2 models I have some validation setup. >> I can get the error_messages_for :model1 to work but not both. Is >> there another way to do it? >> Thanks, >> Scott Burton >> www.similarthings.com >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Scott Burton
2005-Jun-22 21:14 UTC
RE: Multiple model saves, 1 controller to display them all(errors)
Looking in my log I see: [0;37mBEGIN [4;33mUser Load (0.000000) [1;37mSELECT * FROM users WHERE email_address = ''scottfred-+eNEF4B5W7x6hJeZ3vmeHwC/G2K4zDHf@public.gmane.org'' LIMIT 1 [4;35mSQL (0.000000) [0;37mINSERT INTO users (`salt`, `site_id`, `role`, `security_token`, `description`, `token_expiry`, `first_name`, `verified`, `last_name`, `login`, `password`, `email_address`) VALUES(NULL, 0, NULL, NULL, '''', NULL, ''Scott'', 0, ''Burton'', '''', ''db9c93f05d2e41dc2256c3890d5d78ca6e48d418'', ''scottfred-+eNEF4B5W7x6hJeZ3vmeHwC/G2K4zDHf@public.gmane.org'') [4;33mSite Load (0.000000) [1;37mSELECT * FROM sites WHERE site_name = ''freak'' LIMIT 1 [4;35mSQL (0.000000) [0;37mROLLBACK[ So the ROLLBACK is being called however it isn''t rolling back. I am doing dev on my windows box right now and the tables are InnoDB _____ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Russ Smith Sent: Wednesday, June 22, 2005 1:35 PM To: sburton-w+tqpK57T0M7uZOk4t9J8dBPR1lH4CV8@public.gmane.org; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Multiple model saves,1 controller to display them all(errors) Make sure that if your using MySQL that the tables being affected are InnoDB or something else that supports transactions. On Jun 22, 2005, at 1:31 PM, Scott Burton wrote: I am using the transaction method but it is still saving here is my code: In my controller I have def signup if @request.post? @site = Site.new(@params[''site'']) @user = User.new(@params[''user'']) begin @user.transaction do @user.save @site.save raise ActiveRecord::RecordInvalid unless @user.errors.empty? && @site.errors.empty? end #methods that do other stuff.... rescue ActiveRecord::RecordInvalid end else @user = User.new end end much like what the example has on that page. I get the errors for each model but.. if I put in the correct user info and it validates but I don''t put in the correct site info and it fails, it still creates that user in the db. _____ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Russ Smith Sent: Wednesday, June 22, 2005 12:39 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Multiple model saves,1 controller to display them all(errors) This did it for me http://wiki.rubyonrails.com/rails/show/UsefulActionPattern On Jun 22, 2005, at 12:33 PM, Scott Burton wrote: I have a controller that takes the form data and saves to 2 different models. In those 2 models I have some validation setup. I can get the error_messages_for :model1 to work but not both. Is there another way to do it? Thanks, Scott Burton www.similarthings.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails