# Inserting NULL values into PostgreSQL from Rails

**URL:** <https://www.ruby-forum.com/t/inserting-null-values-into-postgresql-from-rails/53277>\
**Category:** Rails\
**Created:** [January 31, 2006, 1:06am UTC](https://www.ruby-forum.com/t/inserting-null-values-into-postgresql-from-rails/53277 "2006-01-31T01:06:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [January 31, 2006, 1:06am UTC](https://www.ruby-forum.com/t/inserting-null-values-into-postgresql-from-rails/53277/1 "2006-01-31T01:06:58Z")

</div>

I have several TIME columns in a PostgreSQL table that I am trying to  
store times of day in. These columns allow NULL values since there  
may some records where there is no time to store.

My problem is that Rails is inserting a default value into these  
fields (the result of calling Time.new) regardless of the value that  
is passed from a form. I am generating select tags with possible time  
values, and I have added one additional one called ‘none’ which I  
want to insert a NULL value. I have tried all kinds of values for  
this option tag, and even tried writing a attribute\_before\_type\_cast  
function and calling it via before\_save to try and reset the value to  
null or nil. I have had no luck.

So I am pretty sure that I am missing something- what do I have to do  
to get Rails to allow an attribute to hold a NULL value and also  
allow me to insert one into the database?

Thanks.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [January 31, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/inserting-null-values-into-postgresql-from-rails/53277/2 "2006-01-31T15:15:46Z")

</div>

You have to do it by hand. At present, rails does not respect nulls  
for any but numeric data.

[http://dev.rubyonrails.org/ticket/3301](http://dev.rubyonrails.org/ticket/3301)

Seems pretty basic. This is 1.0, though…

On 1/31/06, Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> function and calling it via before\_save to try and reset the value to  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
"Her faults were those of her race and sex; her virtues were her own.  
Farewell, and if for ever - "

– “Travels with a Donkey in the Cevennes” by Robert Louis Stevenson
