有人知道避免尝试序列化Date
或Time
对象时发生ActiveJob::SerializationError
的简单方法吗?
我迄今为止的两个解决方案是:
- 在加载参数时调用Marshal/JSON/YAML
dump
,然后在作业中load
回来(这很糟糕,因为我需要猴子补丁邮件发送作业) - 像这样猴子补丁
Date
和Time
:
/lib/core_ext/time.rb
class Time
include GlobalID::Identification
def id
self.to_i
end
def self.find(id)
self.at(id.to_i)
end
end
/lib/core_ext/date.rb
class Date
include GlobalID::Identification
def id
self.to_time.id
end
def self.find(id)
Time.find(id).to_date
end
end
这也很糟糕。有没有更好的解决方案?