A model has only one column, a numeric and the class definition has no constraints define.: create_table "examples"do |t| t.integer "numberid" end in IRB I create a row, save it, and then attempt to assign "abcd" to the attribute. Clearly this is wrong, and in fact the assignment does not take place. I would like however to trap this error, which I cannot seem to do. Example below:>> t = Example.create(:numbid => 4)=> #<Example id: 3, numberid: 4>>> t.numberid = "mmmm"=> "mmmm">> t.numberid=> 0>> t.numberid_before_type_cast=> "mmmm">>Is there a configuration option to make this kind of assignment "noisy" in some way so I can trap the error and provide a meaningful message to the user? Thanks for any help, --Kip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sean Allen
2008-Jan-27 13:27 UTC
Re: Assigning wrong data type to attribute: how to trap error
you can add validations to your model. On Jan 27, 2008, at 7:15 AM, Kip wrote:> > A model has only one column, a numeric and the class definition has no > constraints define.: > > create_table "examples"do |t| > t.integer "numberid" > end > > in IRB I create a row, save it, and then attempt to assign "abcd" to > the attribute. Clearly this is wrong, and in fact the assignment does > not take place. I would like however to trap this error, which I > cannot seem to do. Example below: > >>> t = Example.create(:numbid => 4) > => #<Example id: 3, numberid: 4> >>> t.numberid = "mmmm" > => "mmmm" >>> t.numberid > => 0 >>> t.numberid_before_type_cast > => "mmmm" >>> > > Is there a configuration option to make this kind of assignment > "noisy" in some way so I can trap the error and provide a meaningful > message to the user? > > Thanks for any help, > > --Kip > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Sean, I understand that and do it. Just surprised to find that the actual assignmnent doesn''t fail, and does return the bogus value as the result of the assignment. So to error processing: my validation message string is: "Must be a number between -180 and 180, not ''#{self.latitude_before_type_cast}''" However it appears that the *_before_type_cast methods (indeed any attribute methods) aren''t available in the scope of validations. Any ideas how to access these methods in the message string? Or alternative suggestions on how to achieve the objective of having a meaningful message including the wrong text? Regards, --Kip On Jan 28, 12:27 am, Sean Allen <s...-j6/ZtAoqnBr6dDqEjws1Cg@public.gmane.org> wrote:> you can add validations to your model. > > On Jan 27, 2008, at 7:15 AM, Kip wrote: > > > > > A model has only one column, a numeric and the class definition has no > > constraints define.: > > > create_table "examples"do |t| > > t.integer "numberid" > > end > > > in IRB I create a row, save it, and then attempt to assign "abcd" to > > the attribute. Clearly this is wrong, and in fact the assignment does > > not take place. I would like however to trap this error, which I > > cannot seem to do. Example below: > > >>> t = Example.create(:numbid => 4) > > => #<Example id: 3, numberid: 4> > >>> t.numberid = "mmmm" > > => "mmmm" > >>> t.numberid > > => 0 > >>> t.numberid_before_type_cast > > => "mmmm" > > > Is there a configuration option to make this kind of assignment > > "noisy" in some way so I can trap the error and provide a meaningful > > message to the user? > > > Thanks for any help, > > > --Kip--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---