Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 计算运行时间并保存到数据库_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 计算运行时间并保存到数据库

Ruby on rails 计算运行时间并保存到数据库,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我正在尝试登录Ruby app DB Table MySQL数据库,记录导入脚本开始、完成的时间和日期,以及脚本执行所用的时间 开始时间和完成时间的日志记录非常简单,我已经让它工作了,但是,我无法让它在表中保存运行时,它只是跳过了字段 下面是迁移文件中的表结构 create_table :import_alls do |t| t.datetime :Import_Start_Time t.datetime :Import_Finish_Time t.time :Import_Dura

我正在尝试登录Ruby app DB Table MySQL数据库,记录导入脚本开始、完成的时间和日期,以及脚本执行所用的时间

开始时间和完成时间的日志记录非常简单,我已经让它工作了,但是,我无法让它在表中保存运行时,它只是跳过了字段

下面是迁移文件中的表结构

create_table :import_alls do |t|
  t.datetime :Import_Start_Time
  t.datetime :Import_Finish_Time
  t.time :Import_Duration


  t.timestamps
end
这是我的密码

@script_start_time = Time.now
[... Script ...]
@script_finish_time = Time.now
@script_runtime = @script_finish_time - @script_start_time

import = ImportAll.new
    import.Import_Start_Time = @script_start_time # Saves correctly
    import.Import_Finish_Time = @script_finish_time # Saves Correctly
    import.Import_Duration = @script_runtime # Skips
    [... a few other fields to save ...] # All save correctly
import.save!

puts @script_runtime # Puts correct number of seconds as a line in the server log
保存的结果:

Import_Start_Time = Start Date & Time as expected
Import_Finish_Time = Finish Date & Time as expected
Import_Duration = NULL (expected duration expressed as HH:MM:SS)
所以

时间字段是否为正确的if字段类型?还是应该是约会时间

如何将@script_运行时值转换为时间字段,以便将其保存在表中


可能是某些数据类型转换问题:

2.6.3 :018 > t = Time.new(12.023267542)
=> 0012-01-01 00:00:00 +0053 

我用整数列代替时间来存储持续时间。

你使用什么数据库?只是编辑了一个问题以反映它是MySQL数据库。你确定@script\u runtime不是零吗?当我检查服务器日志时,put@script\u runtime行输入了正确的秒数,所以是的,可以肯定的是,当前值是12.023267542,这很可能就是问题所在,但是,我需要它在管理面板视图中轻松显示为时间。如果您能建议我如何做到这一点,我可以很容易地更改存储类型。