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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.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 Rails在持久化时更改YAML_Ruby On Rails_Serialization - Fatal编程技术网

Ruby on rails Rails在持久化时更改YAML

Ruby on rails Rails在持久化时更改YAML,ruby-on-rails,serialization,Ruby On Rails,Serialization,TL;DR:我在Rails 5.1中有一个ActiveRecord模型,它有一个复合字段,在MySQL中序列化为YAML到一个文本字段。我想要以一种非常具体的方式格式化日期字段,但Rails在写入DB时试图变得聪明,我不明白为什么 详细信息 我试图坚持一个非常简单的模型 class ReportService < ApplicationRecord # AR associations omitted serialize :report_params, ReportParamsSe

TL;DR:我在Rails 5.1中有一个ActiveRecord模型,它有一个复合字段,在MySQL中序列化为YAML到一个文本字段。我想要以一种非常具体的方式格式化日期字段,但Rails在写入DB时试图变得聪明,我不明白为什么

详细信息

我试图坚持一个非常简单的模型

class ReportService < ApplicationRecord
  # AR associations omitted

  serialize :report_params, ReportParamsSerializer
end
这里的
report\u params
只是一些散列:

{"title"=>nil, "date_from"=>"2018-05-02 04:00:00", "date_to"=>"2018-05-03 03:59:00", "output"=>"PDF", ...}
将输出打印到console会得到我想要的YAML:

---
title: 
date_from: 2018-05-02 04:00:00 
date_to: 2018-05-03 03:59:00 
...
但是,当我查看数据库时,我看到:

---
title: 
date_from: 2018-05-02 05:00:00.000000000 +01:00
date_to: 2018-05-03 04:59:00.000000000 +01:00
....
知道为什么会这样吗

编辑:我添加了更多日志记录语句,以显示何时调用序列化程序

  def self.dump(report_params)
    yaml_string = report_params.to_yaml
    puts "presubstitution: #{yaml_string}"
    quoted_date_regex = /\'(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)\'/
    yaml_string.gsub!(quoted_date_regex, '\1 ')
    puts "post_substitution: #{yaml_string}"
    yaml_string
  end
运行

puts "report args: #{report_args.inspect}"
puts "as YAML:"
puts ReportParamsSerializer.dump(report_args[:report_params])
ReportService.create(report_args)
为我提供如下输出:

as YAML:
presubstitution: ---
title: 
date_from: '2018-05-01 23:00:00'
date_to: '2018-05-02 22:59:00'
...
post_substitution: ---
title: 
date_from: 2018-05-01 23:00:00 
date_to: 2018-05-02 22:59:00 
---
title: 
date_from: 2018-05-01 23:00:00 
date_to: 2018-05-02 22:59:00 
...
presubstitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00
...
post_substitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00
...
  SQL (47.7ms)  INSERT INTO `report_services` (other_stuff, `report_params`, ) VALUES (other_values, '---\ntitle: \ndate_from: 2018-05-02 00:00:00.000000000 +01:00\ndate_to: 2018-05-02 23:59:00.000000000 +01:00\ ...)
presubstitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00
...
post_substitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00

能否在
yaml\u string=report\u params.to\u yaml
之后提供输出?为了消除yaml已经在序列化您的时间的可能性,如上所述,但这表明序列化被调用了好几次,使用不同的输入,所以谢谢。。。
as YAML:
presubstitution: ---
title: 
date_from: '2018-05-01 23:00:00'
date_to: '2018-05-02 22:59:00'
...
post_substitution: ---
title: 
date_from: 2018-05-01 23:00:00 
date_to: 2018-05-02 22:59:00 
---
title: 
date_from: 2018-05-01 23:00:00 
date_to: 2018-05-02 22:59:00 
...
presubstitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00
...
post_substitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00
...
  SQL (47.7ms)  INSERT INTO `report_services` (other_stuff, `report_params`, ) VALUES (other_values, '---\ntitle: \ndate_from: 2018-05-02 00:00:00.000000000 +01:00\ndate_to: 2018-05-02 23:59:00.000000000 +01:00\ ...)
presubstitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00
...
post_substitution: ---
title: 
date_from: 2018-05-02 00:00:00.000000000 +01:00
date_to: 2018-05-02 23:59:00.000000000 +01:00