# How do I set the default value for date fields?

**URL:** <https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628>\
**Category:** Rails\
**Created:** [January 4, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628 "2006-01-04T18:39:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 4, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628/1 "2006-01-04T18:39:34Z")

</div>

I want my date fields to default to NIL or “”, because the presence of a  
date means something. My problem is that rails is defaulting the fields  
to today’s date automatically when I create a new record through the  
scaffold. I tried removing the default ‘0000-00-00’ and changing NOT  
NULL to NULL, but both columns get today’s date as the default (see  
below for the table’s sql syntax).

So it looks like I need to code something, but I’m unsure to put that in  
the model or in the view (or if this is a form helper deal). Any help  
is appreciated. Thank you in advance!

CREATE TABLE volunteers ( …other columns…  
birthdate date default NULL,  
competency\_in\_service\_on date NOT NULL default ‘0000-00-00’  
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO\_INCREMENT=7 ;

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 4, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628/2 "2006-01-04T18:56:58Z")

</div>

I have not tried this,  
but Chapter 17 (page 361) of Agile Web D. states you can add a  
:include\_blank option to the date\_select

HTH

Frank  
----- Original Message -----  
From: “Sean C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, January 04, 2006 12:39 PM  
Subject: [Rails] How do I set the default value for date fields?

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 4, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628/3 "2006-01-04T19:06:42Z")

</div>

Thanks Frank,  
I was on 371 researching partials. I’ll back track and see if that  
works.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 4, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628/4 "2006-01-04T19:17:16Z")

</div>

got it to work. Below is an example of the code I changed around in  
app/views/admin/\_form.html (Admin is the name of my controller). The  
‘:include\_blank =\> true’ is the part I added. Thanks for your help

Birthdate  
\<%= date\_select 'volunteer', 'birthdate', :include\_blank =\> true %\>\</p

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 4, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628/5 "2006-01-04T19:54:36Z")

</div>

Frank wrote:

> Glad I could help, others help me out all the time.
> 
> Frank  
> ----- Original Message -----  
> From: “Sean C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Sent: Wednesday, January 04, 2006 1:17 PM  
> Subject: [Rails] [Solved]: How do I set the default value for date  
> fields?

I think you can also use :prompt=\>“Select Birthday” instead of  
:include\_blank=\>true

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 4, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/how-do-i-set-the-default-value-for-date-fields/51628/6 "2006-01-04T19:24:06Z")

</div>

Glad I could help, others help me out all the time.

Frank  
----- Original Message -----  
From: “Sean C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, January 04, 2006 1:17 PM  
Subject: [Rails] [Solved]: How do I set the default value for date  
fields?
