2015-04-22 48 views
1

我發送電子郵件通知根據我的條目表rake任務:我有發送的電子郵件通知rake任務哪能組他們

如果approve_disapprove有一個「3」(3個意思是「待定'),它會發送一封電子郵件給批准條目的人,但條目仍處於待定狀態。

這樣做的問題是,如果有多個條目會通過,並找到每個條目都帶有一個3以供approve_disapprove使用,併爲每個條目發送一封電子郵件。

因此,如果我有5個條目,然後當我的任務經過第二天,並且他們仍然標記爲3,它會發送5封電子郵件給審批者。

我該如何對它進行分組,因爲它將所有這些數據組合在一起,基於我的入口表中名爲section的其他列。如果它將所有條目分成3組作爲掛起,並按章節名稱分組,則它只會發送一封包含全部5個請求的電子郵件給該部門的經理?

這裏是具有check_pending任務

def self.check_pending # What this does is goes through each entry and looks at approve_disapprove if its a 3 which is pending it will sent an alert to the employees manager. 
    check_pending = Entry.where(approve_disapprove: 3)      
    check_pending.each do |entry| 
     EntryMailer.check_pending(entry).deliver 
    end 
end 

這是我進入郵件的檢查暫掛

class EntryMailer < ActionMailer::Base 

    def check_pending(entry) 
    @entry = entry 

    mail to: @entry.emp_mail_addr, subject: '(TEST) You have pending time off requests that require approval or disapproval' 
    end 
end 

我的入門車型,這是我check_pending郵件視圖

Hello 

#{@entry.mgr_name} 


The following time off request are pending please approve or disapprove once you have made your decision. 


%li 
    %span.u= @entry.emp_first_name 
    %span.u= @entry.emp_last_name 
%li Dept 
%li= @entry.emp_dept 
%li Leave Start 
%li= @entry.leave_start.strftime('%m/%d/%y') 
%li Leave End 
%li= @entry.leave_end.strftime('%m/%d/%y') 
%li Type Of Request 
%li= @entry.indirect_id 

這是耙子任務

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 

額外的信息

條目表列

table "entries" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "emp_id" 
    t.string "emp_no" 
    t.string "emp_first_name" 
    t.string "emp_last_name" 
    t.string "emp_mail_addr" 
    t.string "indirect_id" 
    t.string "mgr_no" 
    t.string "mgr_first_name" 
    t.string "mgr_last_name" 
    t.string "mgr_mail_addr" 
    t.datetime "leave_start" 
    t.string "employee_type" 
    t.integer "seq_no",   
    t.decimal "range_days",   
    t.string "alt_mgr_no" 
    t.string "alt_mgr_name" 
    t.string "alt_mgr_addr" 
    t.string "emp_dept" 
    t.datetime "leave_end" 
    t.string "approve_disapprove" 
    t.string "section" 

回答

1

這裏的問題是,你需要的條目對象,以獲取有關條目的信息,所以你必須重新編寫邏輯的看法。

首先,定義一對範圍在你的入門型號:

class Entry < ActiveRecord::Base #I'm assuming AR here 
    scope :pending, -> { where(approve_disapprove: 3) } 
    scope :for_section, ->(section) { where(section: section) } 
end 

然後改變你的rake任務到組上段和傳遞的關係,而不是單一的條目:

def self.check_pending 
    sections = Entry.pending.pluck(:section).uniq      
    sections.each do |section| 
     entries = Entry.pending.for_section(section) 
     EntryMailer.check_pending(entries).deliver 
    end 
end 

然後您就需要修改你的郵件:

class EntryMailer < ActionMailer::Base 

    def check_pending(entries) 
    @entries = entries 
    emails = @entries.map(&:emp_mail_addr).uniq.join(',') #may not need this if your email will always be the same, could just grab the first @entries.first.enp_addr 
    mail to: emails, subject: '(TEST) You have pending time off requests that require approval or disapproval' 
    end 
end 

最後您的視圖將需要遍歷通過這些:

Hello 

#{@entries.first.mgr_name} 


The following time off request are pending please approve or disapprove once you have made your decision. 

- @entries.each do |entry| 

    %li 
    %span.u= entry.emp_first_name #note change from @entry to entry 
    ... 
+0

你是真棒!!!!!!!!!!謝謝你的工作完美! @Yule – Snowman88

相關問題