繁体   English   中英

尝试获取rake任务以在以下情况下发送电子邮件通知

[英]Trying to get a rake task to send an email notification if

我的问题是,每次我尝试运行rake check_pending时,我都会得到一个错误。 有人可以帮助我,让我知道我可能做错了什么。 任何帮助是极大的赞赏!

这是我的错误

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)

这是我的模特

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

这是我的任务

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

这是我的时间表

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

这是我的输入表

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

除了这个问题,这里是我的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

尝试这个

Entry.where(approve_disapprove: '3') 

以及为什么3是字符串,您也可以这样使用

Entry.where(approve_disapprove: 3)

您可以循环浏览所有待处理的条目并向其发送电子邮件

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

然后在您的EntryMailer中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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