2017-05-03 29 views
0

調用我的作業時,我得到一個No Method Error。我不知道爲什麼。這是實際的錯誤:ActiveJob中的調用方法執行

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_shopShop模型對象。 get_customers按照預期在此工作之外工作。我似乎無法在這份工作中得到它的工作。

我也試過:

Shop.current_shop.get_customers.perform 

我做了什麼錯?

回答

0

首先,更改perform方法 - 在那裏你只是想執行一些操作,在你的情況下你想調用get_customers上的current_shop對象。那樣做。

class GetCustomersJob < ActiveJob::Base 
    queue_as :default 

    def perform(current_shop) 
    current_shop.get_customers # removed .perform 
    end 
end 

稍後,您要調用該作業。要做到這一點你寫的作業的類名,並使用兩種perform_now立即運行作業:

GetCustomersJob.perform_now(Shop.current_shop) 

perform_later到隊列中的作業供以後:

GetCustomersJob.perform_later(Shop.current_shop) 
+0

是的,仍然得到完全相同的錯誤。我更新了OP,但它的'get_customers'沒有在'job'上定義。這是真的,它在模型上定義。我傳入一個「商店」對象來處理..因此,我的混淆 – ToddT

+0

@ToddT如果你找到了錯誤的原因,我很好奇嗎? –

+0

結果是我的代碼很好,我沒有重新啓動Sidekiq,所以我的代碼改變沒有被拾起。經驗教訓!感謝您的幫助 – ToddT