0

I'm new to writing Rails rake tasks. I want to write a rake task that looks for calls that are within 30.minutes of being due and executing a mailer. Below is the code I would use out of the controller, how can I adapt this to my rake task?

@call = Call.find(params[:id])
if Time.zone.now < @call.transfer_date + 30.minutes
   @call.units.each do |unit|
      CallMailer.cancel_call(unit.incharge, @call).deliver
      CallMailer.cancel_call(unit.attendant, @call).deliver
   end
end

Any help is appreciated.

Sully
  • 14,672
  • 5
  • 54
  • 79
nulltek
  • 3,247
  • 9
  • 44
  • 94

1 Answers1

0
namespace :send_due_emails do
  desc "" #task describtion
  task :send_due_emails_task => :environment do
     send_emails
  end

  def send_emails
    @call = Call.find(params[:id])
    if Time.zone.now < @call.transfer_date + 30.minutes
      @call.units.each do |unit|
         CallMailer.cancel_call(unit.incharge, @call).deliver
         CallMailer.cancel_call(unit.attendant, @call).deliver
      end
    end
  end
end

http://railscasts.com/episodes/66-custom-rake-tasks

http://jasonseifer.com/2010/04/06/rake-tutorial

Sully
  • 14,672
  • 5
  • 54
  • 79
  • I ran rake send_due_emails:send_due_emails_task and got this back: undefined local variable or method `params' for main:Object should I not be using instance variables? – nulltek Oct 01 '12 at 15:34
  • Oh yes. params is not available in the rake task. – Sully Oct 01 '12 at 18:50