Sean Santry
2005-Feb-19 02:42 UTC
[Error] Using validates_presence_of for datetime field causes parse error?
Hi Everyone - I''ve got an odd sort of error when I try to use validates_presence_of for a MySQL ''datetime'' field. Here''s the relevant portion of the table definition: mysql> describe student_surveys; +---------------------+---------------------+------+----- +---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+---------------------+------+----- +---------------------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | <snip> | creation_date | datetime | | | 0000-00-00 00:00:00 | | In my model, I have validates_presence_of :creation_date And the relevant portion of the unit testing ruby file is: def setup @student_survey = StudentSurvey.new #<snip> @student_survey.creation_date = DateTime.now end def test_create @student_survey.save end When I run this unit test, I get: 1) Error: test_create(StudentSurveyTest): NoMethodError: private method `gsub!'' called for #<DateTime: 11776418931730081/4800000000,-5/24,2299161> /usr/lib/ruby/1.8/date/format.rb:238:in `_parse'' /usr/lib/ruby/1.8/parsedate.rb:9:in `parsedate'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ connection_adapters/abstract_adapter.rb:228:in `string_to_time'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ connection_adapters/abstract_adapter.rb:196:in `type_cast'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ base.rb:1054:in `read_attribute'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ base.rb:1031:in `method_missing'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ validations.rb:443:in `send'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ validations.rb:443:in `add_on_empty'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ validations.rb:442:in `each'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ validations.rb:442:in `add_on_empty'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/ validations.rb:378:in `run_validations'' <snip> What gives? If I change my unit test''s setup method to assign DateTime.now.asctime to my creation_date field, it works, but why can''t I just store a DateTime object in the Active Record object''s field? Thanks for any suggestions! - Sean