# Null Text Fields

**URL:** <https://www.ruby-forum.com/t/null-text-fields/50733>\
**Category:** Rails\
**Created:** [December 20, 2005, 5:18pm UTC](https://www.ruby-forum.com/t/null-text-fields/50733 "2005-12-20T17:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dcian](https://www.ruby-forum.com/u/dcian)\
**Post date:** [December 20, 2005, 5:18pm UTC](https://www.ruby-forum.com/t/null-text-fields/50733/1 "2005-12-20T17:18:54Z")

</div>

I tend to allow nulls in cases where the value is unknown, even if  
it’s a text/varchar field. RoR seems to push empty strings into these  
fields. Is there any way to get RoR to use NULL vice ‘’ if the input  
box is empty?

Thanks!

- Ian

---

<div class="post-metadata">

**Author:** ![dcian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dcian](https://www.ruby-forum.com/u/dcian)\
**Post date:** [June 7, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/null-text-fields/50733/2 "2006-06-07T21:28:57Z")

</div>

Ian H. wrote:

> I tend to allow nulls in cases where the value is unknown, even if  
> it’s a text/varchar field. RoR seems to push empty strings into these  
> fields. Is there any way to get RoR to use NULL vice ‘’ if the input  
> box is empty?
> 
> Thanks!
> 
> - Ian

I just found this problem myself. The workaround I developed was to put  
the following into the model object you want this to work for:

before\_validation :set\_empty\_to\_nil

def set\_empty\_to\_nil  
self.attributes.each {|key, value|  
if value.instance\_of?(String) and value == ‘’  
self["#{key}"] = nil  
end  
}  
end

Basically, everytime a create or update is performed, it walks through  
all the attributes, and if its type is “String” and is thus empty, I set  
it to nil. (As I write this, I suppose I could have done “…  
value.empty?”, but anyhoo …)

Of course, this means you have to put it into _every_ model object you  
want this behavior for, so it sort of defeats the DRY principle. There  
may be a better solution (callback object?) other than modifying  
ActiveRecord, but I’m not sure what that would be. I also haven’t  
looked to see if this was filed as a bug or not.

Of course, part of the reason I did this as part of “before\_validation”  
rather than “before\_save” was because I wanted to use a  
“validates\_uniqueness\_of” and not have two empty strings declared equal.  
But as it turns out, ActiveRecord won’t allow two NULLs in a database as  
it considers NULL = NULL (even though it specifically performs an “…  
IS NULL” search). So it was a bit of a moot point.

I can also see where a person may want ‘’ saved to the database rather  
than a NULL. I’d just like the option of turning off that behavior.

---

<div class="post-metadata">

**Author:** ![dcian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dcian](https://www.ruby-forum.com/u/dcian)\
**Post date:** [June 8, 2006, 1:34am UTC](https://www.ruby-forum.com/t/null-text-fields/50733/3 "2006-06-08T01:34:45Z")

</div>

Coey M. wrote:  
before\_validation :set\_empty\_to\_nil

def set\_empty\_to\_nil  
self.attributes.each {|key, value|  
if value.instance\_of?(String) and value == ‘’  
self["#{key}"] = nil  
end  
}  
end

Of course, this means you have to put it into _every_ model object you  
want this behavior for, so it sort of defeats the DRY principle. There  
may be a better solution (callback object?) other than modifying  
ActiveRecord, but I’m not sure what that would be. Put this in a module  
and mix in to each model you want the behavior for.

```
  I also haven't

```

looked to see if this was filed as a bug or not. This seems like things  
working as advertised to me, not a bug.  
If you have a form with fields left empty, those fields will still  
post, they’re just empty.  
And “” != nil in ruby – the first is a String object, the second is  
nil.

So it’s doing exactly what you told it to.

-phil
