bornboulder77-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-14 06:38 UTC
[Rails][Newb] How Rescue DB constraint violation?
Hello People, I''m looking for a short demo of rescuing a DB constraint violation. This is a newby question; my ruby skills are weak. validation in the model is cool but I want to have the safety-net of DB constraints on top of validation. I ran a little experiment to see what Rails does when a DB blocks attempted DML to prevent a contstraint violation. The browser shows a 500 Error. The development log shows this: Symbl Create (0.000000) PGError: ERROR: duplicate key violates unique constraint "symbl_name_unique" : INSERT INTO symbls ("name", "cname") VALUES(''ORCL'', ''oracle2'') SQL (0.000378) ROLLBACK ActiveRecord::StatementInvalid (PGError: ERROR: duplicate key violates unique constraint "symbl_name_unique" Does anyone have a short demo of rescuing a DB constraint violation? I''d like to send a friendly message to the end-user rather than a 500 error. -b --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Look over: http://whytheluckystiff.net/ruby/pickaxe/ Then, wrap your database access in a begin/rescue block. Note that ActiveRecord implicitly wraps database accesses in a transaction that is rolled back on exception. The primary time you will use transactions is when more than one table must be updated to preserve relational integrity. Hope this gets you started along the right path. On Jul 13, 2007, at 11:38 PM, bornboulder77-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > Hello People, > > I''m looking for a short demo of rescuing a DB constraint violation. > > This is a newby question; my ruby skills are weak. > > validation in the model is cool but I want to have the safety-net of > DB constraints > on top of validation. > > I ran a little experiment to see what Rails does when a DB blocks > attempted > DML to prevent a contstraint violation. > > The browser shows a 500 Error. > > The development log shows this: > > Symbl Create (0.000000) PGError: ERROR: duplicate key violates unique > constraint "symbl_name_unique" > : INSERT INTO symbls ("name", "cname") VALUES(''ORCL'', ''oracle2'') > SQL (0.000378) ROLLBACK > > ActiveRecord::StatementInvalid (PGError: ERROR: duplicate key > violates unique constraint "symbl_name_unique" > > Does anyone have a short demo of rescuing a DB constraint violation? > > I''d like to send a friendly message to the end-user rather than a 500 > error. > > -b > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---