ActiveJob 中的调用方法执行
Calling methods in ActiveJob perform
我在给我的工作打电话时收到 No Method Error
。我不知道为什么。这是实际错误:
NoMethodError: undefined method `get_customers' for #
<GetCustomersJob:0x007f15280e4270>
我正在学习 ActiveJob
并创建了我的第一份超级简单的工作,并调用了我的 Shop
模型上定义的方法。作业看起来像这样:
class GetCustomersJob < ActiveJob::Base
queue_as :default
def perform(current_shop)
current_shop.get_customers.perform
end
end
get_customers
在我的商店模型上定义得很好,current_shop
是一个 Shop
模型对象。 get_customers
在这份工作之外按预期工作。我似乎无法让它在这份工作中发挥作用。
我也试过:
Shop.current_shop.get_customers.perform
我做错了什么?
首先,更改 perform
方法 - 在那里您只想执行某些操作,在您的情况下,您想要在 current_shop
对象上调用 get_customers
。那就这样吧。
class GetCustomersJob < ActiveJob::Base
queue_as :default
def perform(current_shop)
current_shop.get_customers # removed .perform
end
end
稍后,您要调用作业。为此,您写下作业的 class 名称并立即使用 perform_now
到 运行 作业:
GetCustomersJob.perform_now(Shop.current_shop)
或 perform_later
将作业排队等待稍后使用:
GetCustomersJob.perform_later(Shop.current_shop)
我忘了在最后用 Test
命名测试 class,所以 perform_now
没有定义。
(为未来的我写这篇文章:wave:)
我在给我的工作打电话时收到 No Method Error
。我不知道为什么。这是实际错误:
NoMethodError: undefined method `get_customers' for #
<GetCustomersJob:0x007f15280e4270>
我正在学习 ActiveJob
并创建了我的第一份超级简单的工作,并调用了我的 Shop
模型上定义的方法。作业看起来像这样:
class GetCustomersJob < ActiveJob::Base
queue_as :default
def perform(current_shop)
current_shop.get_customers.perform
end
end
get_customers
在我的商店模型上定义得很好,current_shop
是一个 Shop
模型对象。 get_customers
在这份工作之外按预期工作。我似乎无法让它在这份工作中发挥作用。
我也试过:
Shop.current_shop.get_customers.perform
我做错了什么?
首先,更改 perform
方法 - 在那里您只想执行某些操作,在您的情况下,您想要在 current_shop
对象上调用 get_customers
。那就这样吧。
class GetCustomersJob < ActiveJob::Base
queue_as :default
def perform(current_shop)
current_shop.get_customers # removed .perform
end
end
稍后,您要调用作业。为此,您写下作业的 class 名称并立即使用 perform_now
到 运行 作业:
GetCustomersJob.perform_now(Shop.current_shop)
或 perform_later
将作业排队等待稍后使用:
GetCustomersJob.perform_later(Shop.current_shop)
我忘了在最后用 Test
命名测试 class,所以 perform_now
没有定义。
(为未来的我写这篇文章:wave:)