ActiveJob 中的 nomethod 错误

nomethod error in ActiveJob

我在 运行 bundle exec rake jobs:work

之后收到以下错误

FAILED (0 prior attempts) with NoMethodError: undefined method `post_it' for # Class:0x00000003375e80>

class PostCreationJob < ActiveJob::Base
  queue_as :default

  def perform(user_id, string, url)
    Message.post_it(user_id, string, url)
  end
end

class Message < ActiveRecord::Base

  def post_it(id, string, url)
    scraper = ForumScraper.new
    scraper.tpt_login(id)
    scraper.make_post(string, url) 
  end      
end

class MessagesController < ApplicationController

   def create
     @message = Message.new(message_params)
     @message.save
     PostCreationJob.perform_later(@message.user_id, @message.string, @message.url)
     redirect_to :back
     flash[:info] = "Posts are being processed."
   end
end

我正在使用 Rails 4.2.5

application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)

module Workspace
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.active_job.queue_adapter = :delayed_job
  end
end

post_it 方法定义为实例方法,但您在调用 Message.post_it 时将其作为 class 方法调用,因此找不到该方法class 并引发无方法异常。如果您将 post_it 定义为 def self.post_it,它将作为 class 方法从 class 访问。