1行のCSVデータでは問題なくzipファイルをモデルに保存できますが、複数行のCSVデータでは
No such file or directoryとなり、zipファイルをモデルに保存することができませんでした。
tmp/以下のディレクトリを実行中に見ると、勝手にPDFファイルが削除されているようでした。
以下、コードになります。ご教授いただけますと幸いです。
ruby
1class SendMaskInsuranceFile < ApplicationRecord 2 mount_uploader :csv_url, DocumentFileUploader 3 mount_uploader :zip_url, ZipFileUploader 4 5 validates :csv_url, presence: true 6 7 def self.generate_mask_insurance_zip_file 8 ActiveRecord::Base.transaction do 9 SendMaskInsuranceFile.where(created_at: Time.current.yesterday..Time.current).each do |send_mask_insurance_file| 10 Tempfile.create(["#{Time.current.to_i}", ".zip"], Rails.root.join('tmp')) do |zip_tempfile| 11 Zip::File.open(zip_tempfile.path, Zip::File::CREATE) do |zipfile| 12 URI.open(send_mask_insurance_file.csv_url.path) do |f| 13 csv = CSV.new(f, headers: :first_row) 14 csv.each do |row| 15 staff_master = StaffMaster.find_by(id: row[5]) 16 employment_file = staff_master.original_insurance_files.where(file_type: :social).order(id: :desc).first 17 social_file = staff_master.original_insurance_files.where(file_type: :employment).order(id: :desc).first 18 if staff_master 19 pdf_string = WickedPdf.new.pdf_from_string( 20 ActionController::Base.new().render_to_string( 21 template: '/admins/send_mask_insurance_files/sended.pdf.slim', 22 layout: 'layouts/pdf.pdf.slim', 23 encoding: 'utf-8', 24 locals: { 25 postal_code: row[3], 26 address: row[4], 27 company_name: row[2], 28 department_name: row[1], 29 employment_file: employment_file.blank? ? nil : embed_remote_image(employment_file.url.path, 'image/jpeg'), 30 social_file: social_file.blank? ? nil : embed_remote_image(social_file.url.path, 'image/jpeg'), 31 } 32 ) 33 ).force_encoding("UTF-8") 34 35 Tempfile.create(["#{staff_master.id}-#{Time.current.to_i}", ".pdf"], Rails.root.join('tmp')) do |mask_insurance_tempfile| 36 mask_insurance_tempfile.write pdf_string 37 zipfile.add(File.basename(mask_insurance_tempfile.path), mask_insurance_tempfile.path) 38 end 39 end 40 end 41 end 42 end 43 send_mask_insurance_file.update!(zip_url: zip_tempfile) 44 end 45 end 46 end 47 end 48 49 def self.embed_remote_image(url, content_type) 50 asset = URI.open(url, "r:UTF-8", &:read) 51 base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "") 52 "data:#{content_type};base64,#{Rack::Utils.escape(base64)}" 53 end 54end
実行コマンド
bundle exec rake cron:generate_mask_insurance_zip_file
エラーメッセージ
[START] [{:generate_mask_insurance_zip_file=>:environment}] (2022/09/26 09:28) [ERROR] No such file or directory @ rb_sysopen - /Users/●●/Dropbox/sites/app/tmp/02393164-166415211720220926-1793-w6nj56.pdf (2022/09/26 09:28) rake aborted! Errno::ENOENT: No such file or directory @ rb_sysopen - /Users/●●/Dropbox/sites/app/tmp/02393164-166415211720220926-1793-w6nj56.pdf /Users/●●/Dropbox/sites/app/app/models/send_mask_insurance_file.rb:11:in `block (3 levels) in generate_mask_insurance_zip_file' /Users/●●/Dropbox/sites/app/app/models/send_mask_insurance_file.rb:10:in `block (2 levels) in generate_mask_insurance_zip_file' /Users/●●/Dropbox/sites/app/app/models/send_mask_insurance_file.rb:9:in `block in generate_mask_insurance_zip_file' /Users/●●/Dropbox/sites/app/app/models/send_mask_insurance_file.rb:8:in `generate_mask_insurance_zip_file' /Users/●●/Dropbox/sites/app/lib/tasks/cron.rake:98:in `block (2 levels) in <main>' /Users/●●/Dropbox/sites/app/lib/tasks/cron.rake:7:in `block in task_with_logger_and_notify' /Users/●●/.rbenv/versions/2.7.6/bin/bundle:23:in `load' /Users/●●/.rbenv/versions/2.7.6/bin/bundle:23:in `<main>' Tasks: TOP => cron:generate_mask_insurance_zip_file (See full trace by running task with --trace)
エラーメッセージは全文載せてください
ありがとうございます。
エラーメッセージを追加いたしました。
send_mask_insurance_file.rb:10 って
zip_tempfile = Tempfile.new(["#{Time.current.to_i}", ".zip"], Rails.root.join('tmp'))
ですか?
10行目は以下になります。
Zip::File.open(zip_tempfile.path, Zip::File::CREATE) do |zipfile|
とすると、エラーメッセージが出たのは載せてある class SendMaskInsuranceFile とは別物ですね。
8行目が generate_mask_insurance_zip_file
9行目が block in generate_mask_insurance_zip_file
だと言ってますから
載せてるfileだとすると 10行目は SendMaskInsuranceFile.where(created_at:
になりますが。
申し訳ございません。
余計な改行が入っていました。
正しくは11行目が以下になります。
Zip::File.open(zip_tempfile.path, Zip::File::CREATE) do |zipfile|
内容も修正いたしました。
