Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.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 缓存日期早于1900的模型时出现编组错误_Ruby On Rails_Ruby_Marshalling - Fatal编程技术网

Ruby on rails 缓存日期早于1900的模型时出现编组错误

Ruby on rails 缓存日期早于1900的模型时出现编组错误,ruby-on-rails,ruby,marshalling,Ruby On Rails,Ruby,Marshalling,我有一些活动记录模型具有“published_on”属性。当我尝试缓存日期早于1900年1月1日发布的模型时,会出现以下错误: Marshalling error for key 'popular_products': year too big to marshal: 1300 UTC You are trying to cache a Ruby object which cannot be serialized to memcached. 我可以用ruby重现类似的错误: irb(main)

我有一些活动记录模型具有“published_on”属性。当我尝试缓存日期早于1900年1月1日发布的模型时,会出现以下错误:

Marshalling error for key 'popular_products': year too big to marshal: 1300 UTC
You are trying to cache a Ruby object which cannot be serialized to memcached.
我可以用ruby重现类似的错误:

irb(main)> Marshal.dump( Time.parse("1/1/1900") )
ArgumentError: year too big to marshal: 1899 UTC

缓存日期早于1900的模型的正确方法是什么

正如@j03w所评论的,这是时间类型的限制。我选择将类型更改为最新,这解决了问题

您还可以重载Time类的_dump和_load方法,如下所示:

require 'time' # Time extension to have xmlschema method
class Time
  def _dump(level)
    xmlschema(9) # store nsec
  end
  def self._load(str)
    Time.parse(str)
  end
end

irb(main):013:0> t=Time.new(1515,1,1)
=> 1515-01-01 00:00:00 +0100
irb(main):014:0> a=Marshal.dump(t)
=> "\x04\bIu:\tTime(1515-01-01T00:00:00.000000000+01:00\x06:\x06ET"
irb(main):015:0> t==Marshal.load(a)
=> true

嗯,你的ruby版本是什么,对我来说很好。该语句的输出:
“\x04\bIu:\tTime\r%\x00\x00\x80\x00\x00\x00\x06:\voffseti\xFE\xB0\xB9”
这是1.9.3的最新版本,这是2.0的最新版本,除了确认限制之外,对您没有任何帮助……啊,谢谢@j03w-确认了这一点。解决方案可能是使用日期,而不是时间:Marshal.dump(Date.parse(“1/1/100”))只起作用fine@j03w如果您想回答,我很乐意接受。当我使用您的代码并运行producation env时,我会得到:“rails没有时间信息在“K\x86\x1C\x80\x00\x00\xE0\x00”中”