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.
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?
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.
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.