我尝试在我的数据库中请求日期时间条目,其中包含ruby on rails。这在Linux环境中工作正常,但在Windows环境中却出现NoMethodError。调试时,我发现将datetime字符串转换为时间时,错误发生在schema_definitions.rb的第201行

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


当看$ 7时,我得到零,似乎$ 7没有初始化。在这种情况下,7美元是多少?我的绳子是

2011-04-01 10:48:57


这是在应包含日期的数组条目上调用validate之后的跟踪部分

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 `match_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?'

最佳答案

格式:: ISO_DATETIME是一个正则表达式:
/ \ A(\ d {4})-(\ d \ d)-(\ d \ d)(\ d \ d):(\ d \ d):(\ d \ d)(。\ d +)? \ z /

第7次捕获是小数秒(例如“ .123”),该秒被转换为浮点数,然后转换为微秒。在您的字符串中,$ 7将为nil,但这不是问题,因为nil.to_f仅为0.0。
尝试在控制台中执行nil.to_f以确保得到0.0。

您确定NoMethodError是由该行引起的吗?您可以发布回溯吗?

10-07 17:51