2011-11-15 51 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