# Set empty values as null in the database

**URL:** <https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341>\
**Category:** Rails\
**Created:** [November 17, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341 "2006-11-17T18:40:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 17, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341/1 "2006-11-17T18:40:51Z")

</div>

Le’s say that I have a table called people and the column middle\_name is  
nullable. If the user enters say blank or empty space then in the  
database  
it is recorded as empty space. I would like in all my models all empty  
spaces to be recorded as null.

I guess I can write a plugin which will do so for all the models but I’m  
sure something like that should already be existing.I checked but  
couldn’t  
find anything. Anyone knows any plugin which might be of help.

Thanks  
-=-

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 17, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341/2 "2006-11-17T18:48:53Z")

</div>

On 11/17/06, Neeraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -=-
> 
> >

I think you should be able to use a before\_save hook in you person  
model to change empty strings to nil… or do it application wide with  
an extention to AR.

class ActiveRecord::Base

def before\_save  
attributes.each { |key, value| write\_attribute(key, nil) if  
value.is\_a?(String) && value.blank? }  
end

end

Hope this helps.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 17, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341/3 "2006-11-17T19:27:05Z")

</div>

Neeraj K. wrote:

> Le’s say that I have a table called people and the column middle\_name is  
> nullable. If the user enters say blank or empty space then in the  
> database it is recorded as empty space. I would like in all my models  
> all empty spaces to be recorded as null.

WhitespaceKiller does this

[http://wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields](http://wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 17, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341/4 "2006-11-17T21:09:22Z")

</div>

I’m still learning ruby and my question is:

If a model is using before\_save method in the fashion you have  
illustrated  
then this before\_save method would be overriden by the before\_save  
method of  
the model and empty string will be persisted.

-=-

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 17, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/set-empty-values-as-null-in-the-database/76341/5 "2006-11-17T21:33:25Z")

</div>

On 11/17/06, Neeraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > find anything. Anyone knows any plugin which might be of help.
> > 
> > –  
> > Zack C.  
> > [http://depixelate.com](http://depixelate.com)
> > 
> > > >

Neeraj,

That is true - but the model can also simply call super() from the  
before\_save() to keep this functionality.

There are also a bunch of callbacks you can choose from:  
[http://caboo.se/doc/classes/ActiveRecord/Callbacks.html](http://caboo.se/doc/classes/ActiveRecord/Callbacks.html)

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)
