Hi List When using the create and update methods, is there anyway of confirming that the create/update method was unsuccessful- e.g. if it failed validation? The API documentation and playing around in the console suggest that it fails silently. With a create, I could always check if the object''s id has been set - but what would one do on an update? Any help appreciated Rory --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10 Apr 2008, at 13:28, Rory McKinley wrote:> > Hi List > > When using the create and update methods, is there anyway of > confirming > that the create/update method was unsuccessful- e.g. if it failed > validation? The API documentation and playing around in the console > suggest that it fails silently. > > With a create, I could always check if the object''s id has been set - > but what would one do on an update?They return true/false. the ! variants raise an exception instead. You can always call the valid? method to see if an object''s validations pass. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
simply check if save or update_attributes returns true: if @alert.save flash[:notice] = ''cool, its ok'' redirect_to wherever else flash[:notice] = ''oops!'' end same for update_attributes and of course you can use render :template => instead of redirect or redirect in case of errors another way would be to check the errors object of the record if @alert.errors.size > 0 ... the error object is handy anyway, since it will contain all those errors from your validations -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> > On 10 Apr 2008, at 13:28, Rory McKinley wrote:<snip>>> With a create, I could always check if the object''s id has been set - >> but what would one do on an update? > > They return true/false. the ! variants raise an exception instead. You > can always call the valid? method to see if an object''s validations > pass. > > Fred<snip> Hi Fred Thanks for that - wasn''t aware there is a create! and update!. Just a note - according to the api docs: create(attributes = nil) Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not. But I will just catch the exception from create! and update!. Thanks for the help --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote: <snip>> > if @alert.errors.size > 0 ... > the error object is handy anyway, since it will contain all those errors > from your validations<snip> Thanks Thorsten So , would it be recommended to use this method (checking @alert.errors) versus catching an exception from the save! or create! or update! ? Thanks Rory --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> So , would it be recommended to use this method (checking @alert.errors) > versus catching an exception from the save! or create! or update! ? >we mostly use the "if @alert.save then" version Freds way with the exceptions is good too of course just a matter of taste (or maybe Fred can enlighten us, if it has additional advantages). i just like to handle errors where they happen to be and with the rescue handling for exceptions you end up with a bit more code, i think i mentioned the errors object mainly because it''s another alternative and useful for other purposes (show errors to the user, logger.info errors for debugging) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote: <snip>> if @alert.errors.size > 0 ... > the error object is handy anyway, since it will contain all those errors > from your validations<snip> One more question ;) - is there anyway that the method could have completed and produced errors? Or is errors.size > 1 a guarantee that the operation failed? R --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---