chris johnson
2007-Sep-07 07:08 UTC
My controller became fat trying to handle multiple models - need guidance please
Hello all. I am experiencing something that has to be fairly common in the rails world, but I haven''t found a clean solution yet. I have a SignupController that handles multiple models in a single form. Those models are User, UserProfile, CreditCard, Subscription, and Order. When the form is filled out and posted, I want to do exactly what I would do if the form was only for a single model: (1) Build the object from params[:model] (2) Save the object, rendering errors to the form page if they exists (otherwise flash ''Success''). Why is this so tough when dealing with multiple models? Here is what I am doing (and this is how I know it''s too complicated and there must be a better way): (1) Building each object (@user = User.new(params[:user]), @cc CreditCard.new(params[:cc]), etc). (2) http://pastie.caboo.se/94819 (3) I haven''t gotten to manually building an ''errors'' hash yet. I would love to hear some feedback. Many thanks, Chris --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter De Berdt
2007-Sep-07 08:04 UTC
Re: My controller became fat trying to handle multiple models - need guidance please
On 07 Sep 2007, at 09:08, chris johnson wrote:> I am experiencing something that has to be fairly common in the rails > world, but I haven''t found a clean solution yet. > > I have a SignupController that handles multiple models in a single > form. > Those models are User, UserProfile, CreditCard, Subscription, and > Order. > When the form is filled out and posted, I want to do exactly what I > would do if the form was only for a single model: > (1) Build the object from params[:model] > (2) Save the object, rendering errors to the form page if they exists > (otherwise flash ''Success''). > > Why is this so tough when dealing with multiple models? Here is what > I > am doing (and this is how I know it''s too complicated and there must > be > a better way): > (1) Building each object (@user = User.new(params[:user]), @cc > CreditCard.new(params[:cc]), etc). > (2) http://pastie.caboo.se/94819 > (3) I haven''t gotten to manually building an ''errors'' hash yet.Seems all of other the models are related to the User model, you should look into validates_associated: http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ ClassMethods.html#M000948 Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
chris johnson
2007-Sep-08 17:58 UTC
Re: My controller became fat trying to handle multiple models - need guidance please
Thanks for the feedback Peter.>From what I gather, validates_associated does not give specific errorfeedback per model, so that is less than ideal. I have recently learned that error_messages_for can take in multiple models, so that is helpful. Still, I find dealing with multiple models in one form/ controller to be a bit cumbersome, especially compared to how smooth everything else in rails is :-) Cheers, Chris On Sep 7, 1:04 am, Peter De Berdt <peter.de.be...-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote:> On 07 Sep 2007, at 09:08, chris johnson wrote: > > > > > > > I am experiencing something that has to be fairly common in the rails > > world, but I haven''t found a clean solution yet. > > > I have a SignupController that handles multiple models in a single > > form. > > Those models are User, UserProfile, CreditCard, Subscription, and > > Order. > > When the form is filled out and posted, I want to do exactly what I > > would do if the form was only for a single model: > > (1) Build the object from params[:model] > > (2) Save the object, rendering errors to the form page if they exists > > (otherwise flash ''Success''). > > > Why is this so tough when dealing with multiple models? Here is what > > I > > am doing (and this is how I know it''s too complicated and there must > > be > > a better way): > > (1) Building each object (@user = User.new(params[:user]), @cc > > CreditCard.new(params[:cc]), etc). > > (2)http://pastie.caboo.se/94819 > > (3) I haven''t gotten to manually building an ''errors'' hash yet. > > Seems all of other the models are related to the User model, you > should look into validates_associated: > > http://rails.rubyonrails.com/classes/ActiveRecord/Validations/ > ClassMethods.html#M000948 > > Best regards > > Peter De Berdt- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---