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?


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs