gpt4 book ai didi

ruby-on-rails - after_create 回调中的 ActiveJob::DeserializationError - 未找到记录

转载 作者:行者123 更新时间:2023-12-05 08:22:35 25 4
gpt4 key购买 nike

在 Rails 6 应用程序中,我有以下代码。

class Reservation < ApplicationRecord
after_create :publish_creation

def publish_creation
Publishers::Reservations::CreateJob.perform_later(self)
end
end

class Publishers::Reservations::CreateJob < ApplicationJob
queue_as :default

def perform(reservation)
puts reservation.inspect #dummy code for testing
end
end

大多数时候我创建一个Reservation(记录总是在数据库中创建),我得到下面的错误

2020-02-14T09:54:03.707Z pid=81787 tid=1xmj WARN: ActiveJob::DeserializationError: Error while trying to deserialize
arguments: Couldn't find Reservation with 'id'= 35651cf7-35bc-4da0-bb0d-6285ac093d22

Sidekiq 第一次尝试处理作业时,它总能找到 Reservation 并且一切正常。

Reservation id: "35651cf7-35bc-4da0-bb0d-6285ac093d22", analysis_id:
"6b3b167b-1279-49c0-991a-b580c375fd0f", reservable_type: "User",
reservable_id: "94f60c16-29d4-4372-983b-7544c393a7e6",
reserved_between: 2020-02-10 08:00:00 UTC..2020-02-10 08:10:00 UTC,
state: "scheduled", created_at: "2020-02-14 10:02:28", updated_at:
"2020-02-14 10:02:28"

我在这里遗漏了什么吗?这是否与我在 development 模式下运行有关,一旦我转到 production 它应该消失?

最佳答案

您应该使用 after_commit 而不是 after_create。这是因为

after_create - Is called after Model.save on new objects that haven‘t been saved yet (no record exists). In the case of after_create, this will always be before the call to save (or create) returns. Rails wraps every save inside a transaction and the before/after create callbacks run inside that transaction.

after_commit - Is called after the database transaction is completed. With after_commit, your code doesn't run until after the outermost transaction was committed.

关于ruby-on-rails - after_create 回调中的 ActiveJob::DeserializationError - 未找到记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60224142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com