2

csv ファイルを生成してメーラーに送信する以下の resque ジョブがあります。csv ファイルにデータがあることを確認したいので、空のファイルをメールで送信しません。なぜかPerformメソッドの外にメソッドを書くとうまくいきません。たとえば、csv ファイルの最初の行にデータがあることがわかっている場合、以下のコードは無効と出力されます。以下の行のコメントを外すと、正常に機能することを確認できますが、このファイルのチェックを別のメソッドに抽出したいと考えています。これは正しいです?

class ReportJob
  @queue = :report_job

def self.perform(application_id, current_user_id)
 user = User.find(current_user_id)
 client_application = Application.find(client_application_id)
 transactions = application.transactions
 file = Tempfile.open(["#{Rails.root}/tmp/", ".csv"]) do |csv|
   begin
     csv_file = CSV.new(csv)
     csv_file << ["Application", "Price", "Tax"]
     transactions.each do |transaction|
       csv_file << [application.name, transaction.price, transaction.tax]
     end
   ensure
    ReportJob.email_report(user.email, csv_file)
    #ReportMailer.send_report(user.email, csv_file).deliver
     csv_file.close(unlink=true)
   end
 end
end

 def self.email_report(email, csv)
   array = csv.to_a
   if array[1].blank?
     puts "invalid"
   else
     ReportMailer.send_report(email, csv).deliver
   end
 end

end
4

1 に答える 1

0

メソッドを次のように呼び出す必要があります。

ReportJob.email_report(email, csv)

それ以外の場合は、 in を削除selfします。

def self.email_report(email, csv)
   # your implementation here.
end 

メソッドを次のように定義します。

def email_report(email, csv)
  # your implementation.
end

これは、クラス メソッドおよびインスタンス メソッドと呼ばれるものです。

于 2013-01-28T22:44:03.420 に答える