# Datetime field inserted is NULL

**URL:** <https://www.ruby-forum.com/t/datetime-field-inserted-is-null/144532>\
**Category:** Rails\
**Created:** [July 30, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/datetime-field-inserted-is-null/144532 "2008-07-30T18:53:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [July 30, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/datetime-field-inserted-is-null/144532/1 "2008-07-30T18:53:43Z")

</div>

This is pretty baffling. I have the following model object:

create\_table “activities”, :force =\> true do |t|  
t.integer “user\_id”, :limit =\> 11  
t.datetime “created\_at”  
t.datetime “updated\_at”  
t.integer “location\_id”, :limit =\> 11  
t.datetime “event\_date”  
end

model/activity.rb:

class Activity \< ActiveRecord::Base

belongs\_to :User  
belongs\_to :Location

attr\_accessor :event\_date  
attr\_accessor :user  
attr\_accessor :location

end

my activities\_controller.rb:

activity = Activity.new(params[:activity])

activity.event\_date = Date.today

activity.user\_id = current\_user.id

activity.save

It throws an error b/c it’s trying to insert NULL for event\_date in the  
MYSQL database:  
ActiveRecord::StatementInvalid (Mysql::Error: Column ‘event\_date’ cannot  
be null: INSERT INTO `activities` (`update\ d_at`, `location_id`, `user_id`, `event_date`, `created_at`)  
VALUES(‘2008-07-30 05:16:00’, 1, 1, NULL, ‘2008-07-30   
05:16:00’)):

I have no idea why. If I explicitly print out ‘activity.event\_date.to\_s’  
is gives me the date of today.

If I remove the stipulation the “event\_date” can be NULL it just insert  
a NULL value.

thanks in advance for help

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [July 30, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/datetime-field-inserted-is-null/144532/2 "2008-07-30T19:45:57Z")

</div>

Hi Allen,

Allen W. wrote:

> model/activity.rb:
> 
> end

This is definitely just a guess, but I’ve never seen attr\_accessor used  
on  
an ActiveRecord::Base object coming from an MySQL table. I’d speculate  
that  
it may be overriding AR’s behavior somehow, but that’s definitely just a  
guess. What happens if you delete that line from your model?

Bill

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [July 30, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/datetime-field-inserted-is-null/144532/3 "2008-07-30T20:24:26Z")

</div>

Looks like it fixed a few things. Yes I was confused in using the  
“attr\_\*” declaration on an ActiveRecord::Base. Sometimes I wished rails  
would throw a warning of some kind.

Thanks

Bill W. wrote:

> Hi Allen,
> 
> Allen W. wrote:
> 
> > model/activity.rb:
> > 
> > end
> 
> This is definitely just a guess, but I’ve never seen attr\_accessor used  
> on  
> an ActiveRecord::Base object coming from an MySQL table. I’d speculate  
> that  
> it may be overriding AR’s behavior somehow, but that’s definitely just a  
> guess. What happens if you delete that line from your model?
> 
> Bill

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [July 31, 2008, 12:01am UTC](https://www.ruby-forum.com/t/datetime-field-inserted-is-null/144532/4 "2008-07-31T00:01:15Z")

</div>

You’re trying to set a datetime field to a Date object whereas Rails  
would expect a Time object (like Time.now).

You already know you don’t need to (and shouldn’t) make attr\_\*  
declarations for your model’s fields.

You also are using capitalized :User and :Location for your  
belongs\_to. This will fail. They should be lowercase. Also, if a user  
has many activities you can say @activity =  
current\_user.activities.build(params[:activity]) and not have to set  
the user\_id manually.

I’m also not certain why you need a not null constraint in the  
database. Better to use a validates\_presence\_of :event\_date in the  
model.

Rein H.  
Hashrocket  
[http://reinh.com](http://reinh.com)
