1

我无法让 DJ 和 ActionMailer 为我工作。我总是收到这个错误:

rake aborted!
wrong number of arguments (2 for 0..1)
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/arel-3.0.2/lib/arel/expressions.rb:3:in `count'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/delayed_job-3.0.5/lib/delayed/backend/base.rb:81:in `payload_object='
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in `initialize'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/delayed_job-3.0.5/lib/delayed/backend/base.rb:29:in `new'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/delayed_job-3.0.5/lib/delayed/backend/base.rb:29:in `enqueue'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/delayed_job-3.0.5/lib/delayed/message_sending.rb:13:in `method_missing'
/Users/drale2k/Dropbox/htdocs/work/etaxi/code/etaxi/lib/tasks/mailers.rake:6:in `block (2 levels) in <top (required)>'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
/Users/drale2k/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'

这是我的 rake 任务:

namespace :mailer do
    desc "Mail a list of inactive devices"
    task :inactive_devices => :environment do
        @devices = Device.inactive_devices
        # Send only if we have inactive devices
        SystemMailer.delay.inactive_devices(@devices) unless @devices.empty?
    end
end

我的 ActionMailer 类:

class SystemMailer < ActionMailer::Base
  default from: "Report <system@mymail.com>"
  @@recipients = "Me <censored@mail.com>"

  def inactive_devices(devices)
    @devices = devices
    mail(:to => @@recipients, :subject => "Inactive Devices")
  end
end

这是我的设备模型上的“inactive_devices”范围:

scope :inactive_devices, -> { where("pinged_at < ?", 24.hours.ago).where(:silent => false) }

没有 DJ 和延迟方法也能正常工作。

4

1 回答 1

3

您需要在子句.all末尾添加where它才能与delayed_job.

all应该返回一个数组,而where返回一个 ActiveRecord 关系(如果我理解正确的话)。

于 2013-05-22T12:26:50.267 回答