Resque Worker:Worker 中不可用的对象和方法

Resque Worker: Objects and methods not available in worker

我的网络应用程序中有以下工作人员

class TweetSender
    @queue = :tweets_queue 

        def self.perform(tweet_id)
            message = Tweet.where(:status_id => 0).order(:created_at, :id).limit(1)
            message.update_attribute(status_id = 1)
            $client.update(message.text)

        end
end

它被以下 resque-scheduler 调度 tweet_scheduler.yml

tweet_sender:

  cron: "0 20 * * * Europe/Stockholm"
  class: "TweetSender"
  queue: tweets_queue
  args: tweet_id
  description: "This job sends daily tweets from the content db"

由他定义resque.rake

require 'resque/tasks'
require 'resque/scheduler/tasks'
task 'resque:setup' => :environment

namespace :resque do
  task :setup do
    require 'resque'


  end

  task :setup_schedule => :setup do
    require 'resque-scheduler'
    Resque.schedule = YAML.load_file('tweet_schedule.yml')
    require 'jobs'
end
task :scheduler => :setup_schedule
end

在 resque 网络界面中我收到以下错误

Exception
NoMethodError
Error
undefined method `update_attribute' for #<Tweet::ActiveRecord_Relation:0x839ca814>
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:136:in `method_missing'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:99:in `method_missing'
/home/jan/Documents/social/app/jobs/tweet_sender.rb:6:in `perform'

我也试过在没有 update_atttribute 方法的情况下实现 tweet_sender.rb,如下所示:

class TweetSender
    @queue = :tweets_queue 

        def self.perform(tweet_id)
            message = Tweet.where(:status_id => 0).order(:created_at, :id).limit(1)
            message.status_id = 1
            message.save
            $client.update(message.text)

        end
end

并出现以下错误:

Exception
NoMethodError
Error
undefined method `status_id=' for #<Tweet::ActiveRecord_Relation:0x83195bf8>
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:136:in `method_missing'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:99:in `method_missing'
/home/jan/Documents/social/app/jobs/tweet_sender.rb:6:in `perform'

为什么我的方法和标准 rails 方法在我的工作器中不可用?我需要在某处明确要求它们吗?

limit returns ActiveRecord::Relation.

的实例

你真正想要得到的是Tweet模型的一个实例。

使用first:

message = Tweet.where(status_id: 0).order(:created_at, :id).first