# ActiveRecord setting empty strings to nil for Numeric attributes

**URL:** <https://www.ruby-forum.com/t/activerecord-setting-empty-strings-to-nil-for-numeric-attributes/185836>\
**Category:** Rails\
**Created:** [March 26, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/activerecord-setting-empty-strings-to-nil-for-numeric-attributes/185836 "2010-03-26T21:59:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@Chris\_N](https://www.ruby-forum.com/u/Chris_N)\
**Post date:** [March 26, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/activerecord-setting-empty-strings-to-nil-for-numeric-attributes/185836/1 "2010-03-26T21:59:16Z")

</div>

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
