Carl Youngblood
2005-Oct-18 06:03 UTC
How to rescue duplicate entry errors when calling update_attributes
I know this is a simple question but I couldn''t find an answer in the documentation or the rails book. I''m getting an error in a call to update_attributes on a model because I am trying to change a field to a value that already exists and has a unique index. What''s the right way to catch a duplicate entry error and report that to the user in a friendly way? The only exception I get in this case is StatementInvalid so I''m not sure if I am safe in assuming that the error was due to a duplicate entry. Thanks for your help. Carl
Jarkko Laine
2005-Oct-18 15:52 UTC
Re: How to rescue duplicate entry errors when calling update_attributes
On 18.10.2005, at 8.03, Carl Youngblood wrote:> I know this is a simple question but I couldn''t find an answer in the > documentation or the rails book. I''m getting an error in a call to > update_attributes on a model because I am trying to change a field to > a value that already exists and has a unique index. What''s the right > way to catch a duplicate entry error and report that to the user in a > friendly way?validates_uniqueness_of :my_field ? //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Carl Youngblood
2005-Oct-18 18:12 UTC
Re: How to rescue duplicate entry errors when calling update_attributes
Thanks! I had forgotten to add the field in question to that validation. On 10/18/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > On 18.10.2005, at 8.03, Carl Youngblood wrote: > > > I know this is a simple question but I couldn''t find an answer in the > > documentation or the rails book. I''m getting an error in a call to > > update_attributes on a model because I am trying to change a field to > > a value that already exists and has a unique index. What''s the right > > way to catch a duplicate entry error and report that to the user in a > > friendly way? > > validates_uniqueness_of :my_field ?