# Date null when saving model

**URL:** <https://www.ruby-forum.com/t/date-null-when-saving-model/106078>\
**Category:** Rails\
**Created:** [June 6, 2007, 6:56am UTC](https://www.ruby-forum.com/t/date-null-when-saving-model/106078 "2007-06-06T06:56:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [June 6, 2007, 6:56am UTC](https://www.ruby-forum.com/t/date-null-when-saving-model/106078/1 "2007-06-06T06:56:48Z")

</div>

Hi,  
I must be doing something dumb, but I can’t figure this out.

The following code:

customer = Customer.new(:name =\> ‘Ray’, :date\_created =\>  
Time.now.to\_s(:db))  
customer.save!

Always results in the error ActiveRecord::RecordInvalid: Validation  
failed: Date created can’t be null

Yet, this code works fine:  
customer = Customer.new(:name =\> ‘Ray’)  
customer.date\_created = Time.now.to\_s(:db)  
customer.save!

Is there something I’m doing wrong that is preventing me from putting  
everything in the new method?  
Any help would be appreciated.  
Cheers,  
Ray

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [June 6, 2007, 7:49am UTC](https://www.ruby-forum.com/t/date-null-when-saving-model/106078/2 "2007-06-06T07:49:08Z")

</div>

Raymond O’Connor wrote:

> failed: Date created can’t be null
> 
> Yet, this code works fine:  
> customer = Customer.new(:name =\> ‘Ray’)  
> customer.date\_created = Time.now.to\_s(:db)  
> customer.save!
> 
> Is there something I’m doing wrong that is preventing me from putting  
> everything in the new method?

Have you excluded date\_created from mass updates via attr\_accessible  
or attr\_protected? If so, the second method is the only way that  
currently works, however I’d like to see new\_unsafe, create\_unsafe,  
build\_unsafe, attributes\_unsafe=, and update\_attributes\_unsafe methods  
that bypass protection for use in internal processing.

The other alternative is some problem with your Customer.initialize  
code.

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

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [June 6, 2007, 8:33am UTC](https://www.ruby-forum.com/t/date-null-when-saving-model/106078/3 "2007-06-06T08:33:12Z")

</div>

Raymond O’Connor wrote:

> Hi,  
> I must be doing something dumb, but I can’t figure this out.
> 
> The following code:
> 
> customer = Customer.new(:name =\> ‘Ray’, :date\_created =\>

Remember you can use “created\_at” which will insert the date for you  
automatic 🙂
