2011-11-15 63 views
8

我無法調用從包括模塊resque工人內部方法的Rails Resque未定義的方法錯誤。在下面的示例中,當我嘗試調用Worker中的say方法(位於TestLib模塊中)時,我不斷收到未定義的方法錯誤。我已經減少了代碼到裸露的基礎來說明這個問題:在外部模塊

控制器 (/app/controllers/test_controller.rb)

class TestController < ApplicationController 
    def testque 
    Resque.enqueue(TestWorker, "HI") 
    end 
end 

圖書館 (/ lib中/ test_lib。 RB)

module TestLib 
    def say(word) 
    puts word 
    end 
end 

工人 (/工人/ test_worke r.rb)

require 'test_lib' 

class TestWorker 
    include TestLib 

    @queue = :test_queue 

    def self.perform(word) 
    say(word) #returns: undefined method 'say' for TestWorker:Class 
    TestLib::say(word) #returns: undefined method 'say' for TestLib::Module 
    end 
end 

Rake文件 (resque.rake)

require "resque/tasks" 
task "resque:setup" => :environment 

我使用下面的命令運行resque:rake environment resque:work QUEUE='*'

寶石: 軌(3.0.4 ) redis的(2.2.2) redis的命名空間(1.0.3) resque(1.19.0)

服務器: nginx/1.0.6

任何人有任何想法,究竟發生了什麼?

回答

27

當你有一個模塊,它的方法成爲實例方法。當你延伸時,他們會變成階級方法。您只需將include TestLib更改爲extend TestLib即可使用。

+0

* headslap *我想我已經在軌工作了太多最近。那就是訣竅。謝謝! – internetoutfitters

+1

+1 @tbuehlmann。 –

+0

如果你這樣做,它仍然無法正常工作,請重新啓動服務器 – Kathan