# NULL datetime field inserted when value is assigned to model object

**URL:** <https://www.ruby-forum.com/t/null-datetime-field-inserted-when-value-is-assigned-to-model-object/144550>\
**Category:** Rails\
**Created:** [July 30, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/null-datetime-field-inserted-when-value-is-assigned-to-model-object/144550 "2008-07-30T21:06:47Z")\
**Posts on this page:** 1\
**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, 9:06pm UTC](https://www.ruby-forum.com/t/null-datetime-field-inserted-when-value-is-assigned-to-model-object/144550/1 "2008-07-30T21:06:47Z")

</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 "  
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

in a controller I create an "Activiti
