加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – ActiveJob :: SerializationError – 不支持的参数类型

发布时间:2020-12-16 23:29:15 所属栏目:百科 来源:网络整理
导读:我使用Rails 5和ActiveJob来处理后台任务.我试图将使用as_json序列化的对象传递给我的工作但是我收到以下错误: ActiveJob::SerializationError (Unsupported argument type: Time):ActiveJob::SerializationError (Unsupported argument type: DateTime):
我使用Rails 5和ActiveJob来处理后台任务.我试图将使用as_json序列化的对象传递给我的工作但是我收到以下错误:
ActiveJob::SerializationError (Unsupported argument type: Time):
ActiveJob::SerializationError (Unsupported argument type: DateTime):

我知道ActiveJob不会采用Time / DateTime对象,因为某些排队系统不处理这些类型.所以我试图序列化的对象如下:

card = Card.first
=> #<Card id: 256,title: "quis",description: "Sunt corporis error laudantium veritatis impedit r...",due_date: "2016-12-15 12:00:00",slug: "quis",created_at: "2016-11-30 17:00:01",updated_at: "2016-11-30 17:00:01",list_id: 26,position: 0,period_type: "hours",period_length: 0.0,user_id: 1>

当我跑:

card.as_json
=> {"id"=>256,"title"=>"quis","description"=>"Sunt corporis error laudantium veritatis impedit repellat quasi.","due_date"=>Wed,15 Dec 2016 12:00:00 UTC +00:00,"slug"=>"quis","created_at"=>Wed,30 Nov 2016 17:00:01 UTC +00:00,"updated_at"=>Wed,"list_id"=>26,"position"=>0,"period_type"=>"hours","period_length"=>0.0,"user_id"=>1}

created_at,updated_at和due_date都是ActiveSupport :: TimeWithZone的实例

我试图通过在初始化程序中使用以下代码来覆盖它,我在另一个SO帖子中找到但是它没有帮助:

class ActiveSupport::TimeWithZone
    def as_json(options = {})
        strftime('%Y-%m-%d %H:%M:%S')
    end
end

当as_json在对象上运行时,任何人都可以帮助将日期作为字符串吗?

解决方法

请改用card.to_json.它将返回作业将能够接受的JSON字符串.

如果你有逻辑你想与as_json一起使用链接to_json到as_json的调用结束,即card.as_json.to_json.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读