Chris N
2010-Mar-26 17:13 UTC
ActiveRecord setting empty strings to nil for Numeric attributes
I have an ActiveRecord model with several float attributes that takes input directly from the user, and they are all set to not null in the DB. When any of these attributes are set to an empty string (""), their value is instead set to nil. This only occurs with an empty string, setting them to any non-empty string, even just whitespace, sets their values to that of the string. Trying to save a new record with any of these values set to nil results in a column cannot be null error. However, if this is done to a non-new record results in the nil''s being cast back to float before saving. Is there any way to force AR to cast the nil float attributes to float on insert, just like it does on update (other than doing it explicitly in validate or a callback)? Or, is there a to make AR not change an empty string to nil when it is set? 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.