gpt4 book ai didi

ruby-on-rails - ruby 、临时文件、CSV

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:59 25 4
gpt4 key购买 nike

我有以下生成 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

最佳答案

您应该这样调用您的方法:

ReportJob.email_report(email, csv)

否则,去掉 self 在:

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

并按如下方式定义您的方法:

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

这就是我们所说的类方法和实例方法。

关于ruby-on-rails - ruby 、临时文件、CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com