Rails Resque undefined method error in external module

╄→гoц情女王★ 提交于 2019-12-22 04:17:38

问题


I'm having trouble calling methods from an included module inside a resque worker. In the example below, I keep getting undefined method errrors when I attempt to call the say method inside the worker (which is in the TestLib module). I've reduced the code down to bare basics to illustrate the issue:

Controller (/app/controllers/test_controller.rb)

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

Library (/lib/test_lib.rb)

module TestLib
  def say( word )
    puts word
  end
end

Worker (/workers/test_worker.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

Rakefile (resque.rake)

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

I'm running resque using the following command: rake environment resque:work QUEUE='*'

Gems: rails (3.0.4) redis (2.2.2) redis-namespace (1.0.3) resque (1.19.0)

Server: nginx/1.0.6

Anyone have any ideas as to what's going on there?


回答1:


When you include a module, its methods become instance methods. When you extend, they become class methods. You just need to change include TestLib to extend TestLib and it should work.



来源:https://stackoverflow.com/questions/8130071/rails-resque-undefined-method-error-in-external-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!