NoMethodError when requesting datetime element
I try to request a datetime entry in my database with ruby on rails. This works just fine in a linux environment but in my windows environment I get a NoMethodError. When debugging it I found out that the errors occurs at line 201 of the schema_definitions.rb when casting the datetime string to a time
def fast_string_to_time(string)
if string =~ Format::ISO_DATETIME
microsec = ($7.to_f * 1_000_000).to_i <<====== This is line 201
new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
end
end
When watching the $7 I get nil, seems that $7 is not initialized. What is $7 in this context? My string is
2011-04-01 10:48:57
This is the part of the trace after validate is called on an entry of an array that should contain the date
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/schema_definitions.rb:201:in `fast_string_to_time'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/schema_definitions.rb:139:in `string_to_time'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/schema_definitions.rb:78:in `type_cast'
activerecord (3.0.1) lib/active_record/attribute_methods/read.rb:83:in `read_attribute'
activerecord (3.0.1) lib/active_record/base.rb:1558:in `attributes'
activerecord (3.0.1) lib/active_record/base.rb:1558:in `each'
activerecord (3.0.1) lib/active_record/base.rb:1558:in `attributes'
activerecord (3.0.1) lib/active_record/attribute_methods.rb:57:in `attribute_method?'
activemodel (3.0.1) lib/active_model/attribute_methods.rb:394:in `开发者_如何学Pythonmatch_attribute_method?'
activemodel (3.0.1) lib/active_model/attribute_methods.rb:393:in `each'
activemodel (3.0.1) lib/active_model/attribute_methods.rb:393:in `match_attribute_method?'
activemodel (3.0.1) lib/active_model/attribute_methods.rb:378:in `respond_to?'
activerecord (3.0.1) lib/active_record/attribute_methods.rb:52:in `respond_to?'
activerecord (3.0.1) lib/active_record/callbacks.rb:268:in `deprecated_callback_method'
activerecord (3.0.1) lib/active_record/validations.rb:57:in `valid?'
Format::ISO_DATETIME is a Regexp: /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(.\d+)?\z/
The 7th capture is the fractional seconds (e.g. ".123"), which is converted to a float and then converted to microseconds. In your string, $7 would be nil, but that's not a problem, because nil.to_f is just 0.0. Try executing nil.to_f in a console to make sure you get 0.0.
Are you sure the NoMethodError is caused by that line? Can you post the backtrace?
精彩评论