ActiveJob::SerializationError - 不支持的参数类型:时间/日期时间
ActiveJob::SerializationError - Unsupported argument type: Time / 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, 30 Nov 2016 17:00:01 UTC +00:00, "list_id"=>26, "position"=>0, "period_type"=>"hours", "period_length"=>0.0, "user_id"=>1}
created_at、updated_at 和 due_date 都是 ActiveSupport::TimeWithZone
的实例
我试图通过在初始化程序中使用以下代码来覆盖它,这是我在另一个 SO post 中找到的,但它没有帮助:
class ActiveSupport::TimeWithZone
def as_json(options = {})
strftime('%Y-%m-%d %H:%M:%S')
end
end
当对象上的 as_json
为 运行 时,任何人都可以帮助将日期作为字符串吗?
改用card.to_json
。它将 return 作业能够接受的 JSON 字符串。
如果您有想要与 as_json
一起使用的逻辑,只需将 to_json
链接到对 as_json
的调用结束,即 card.as_json.to_json
.
我正在使用 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, 30 Nov 2016 17:00:01 UTC +00:00, "list_id"=>26, "position"=>0, "period_type"=>"hours", "period_length"=>0.0, "user_id"=>1}
created_at、updated_at 和 due_date 都是 ActiveSupport::TimeWithZone
我试图通过在初始化程序中使用以下代码来覆盖它,这是我在另一个 SO post 中找到的,但它没有帮助:
class ActiveSupport::TimeWithZone
def as_json(options = {})
strftime('%Y-%m-%d %H:%M:%S')
end
end
当对象上的 as_json
为 运行 时,任何人都可以帮助将日期作为字符串吗?
改用card.to_json
。它将 return 作业能够接受的 JSON 字符串。
如果您有想要与 as_json
一起使用的逻辑,只需将 to_json
链接到对 as_json
的调用结束,即 card.as_json.to_json
.