Allen Walker
2008-Jul-30 05:56 UTC
datetime field inserted is NULL when value is assigned to model object
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2008-Jul-30 19:18 UTC
Re: datetime field inserted is NULL when value is assigned to model object
Quoting Allen Walker <auswalk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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 >datetime is a Time, not a Date. Try activity.event_date = Time.now. HTH, Jeffrey --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---