简体   繁体   中英

Trying to get a rake task to send an email notification if

My issue is that every time I try to run rake check_pending I get an error. Can someone please help me and let me know what I might be doing wrong. Any help is greatly appreciated!

This is my error

rake check_pending
rake aborted!
NoMethodError: undefined method `find_all_by_approve_disapprove' for Entry (call 'Entry.connection' to establish a connection):Class
/var/lib/gems/1.9.1/gems/activerecord-4.1.8/lib/active_record /dynamic_matchers.rb:26:in `method_missing'
/home/programmer/Test_app/app/models/entry.rb:45:in    `check_pending'
/home/programmer/Test_app/lib/tasks/check_pending.rake:4:in `block in <top (required)>'
Tasks: TOP => check_pending
(See full trace by running task with --trace)

This is my model

class Entry < ActiveRecord::Base

  def self.check_pending #This checks if a time off entry is still pending if so then send an email to the manager reminding them of the pending request.
    check_pending = Entry.find_all_by_approve_disapprove('3')
    if approve_disapporve == '3'
       EntryMailer.check_pending(@entry).deliver
    end
  end
end

This is my rake task

desc "Looks at pending request if the are still marked pending sends a  email to the manager for the request"
task :check_pending => :environment do
Rails.logger.info "Check Pending: Finding all pending."
Entry.check_pending
Rails.logger.info "Found Pending: Check complete."
Rails.logger.flush
end

This is my Schedule

every :day, :at => "9am" do
rake "check_pending"
end

This is my table for entry

  ID    NUMBER(38,0)    
  CREATED_AT    DATE        
  UPDATED_AT    DATE    
  APPROVE_DISAPPROVE    VARCHAR2(255 BYTE)
  EMP_MAIL_ADDR  VARCHAR2 (2555 BYTE)

In addition to this question here is my entry_mailer

class EntryMailer < ActionMailer::Base

  def check_pending(entry)
    @entry = entry

    mail to: entry.emp_mail_addr, subject: '(TEST) You have pending time off request'
  end
end

Try this

Entry.where(approve_disapprove: '3') 

and why 3 is string you can use like this too

Entry.where(approve_disapprove: 3)

You can loop through all the pending entries and send email to them

def self.check_pending
    check_pending = Entry.find_all_by_approve_disapprove(3)
    check_pending.each do |entry|
        EntryMailer.check_pending(entry).deliver
    end 
end

Then in your EntryMailer

def check_pending(entry)
    @entry = entry
    mail to: @entry.emp_mail_addr, subject: '(TEST) You have pending time off request'
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM