Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@vlard's solution is ok. However I've run into some issues with the <code>to_file</code>. It creates a tempfile and the garbage collector deletes (sometimes) the file before it was added to the zip file. Therefor, I'm getting random <code>Errno::ENOENT: No such file or directory</code> errors.</p> <p>So I'm using the following code now (I've kept the initial code variables names for consistency with the initial question)</p> <pre><code>format.zip { registrations_with_attachments = Registration.find_by_sql('SELECT * FROM registrations WHERE abstract_file_name NOT LIKE ""') headers['Cache-Control'] = 'no-cache' #please note that using nanoseconds option in strftime reduces the risks concerning the situation where 2 or more users initiate the download in the same time tmp_filename = "#{RAILS_ROOT}/tmp/tmp_zip_" &lt;&lt; Time.now.strftime('%Y-%m-%d-%H%M%S-%N').to_s &lt;&lt; ".zip" # rubyzip gem version 0.9.4 zip = Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE) zip.close registrations_with_attachments.each { |e| file_to_add = e.file.to_file zip = Zip::ZipFile.open(tmp_filename) zip.add("abstracts/#{e.abstract.original_filename}", file_to_add.path) zip.close puts "added #{file_to_add.path} to #{tmp_filename}" #force garbage collector to keep the file_to_add until after the file has been added to zip } send_data(File.open(tmp_filename, "rb+").read, :type =&gt; 'application/zip', :disposition =&gt; 'attachment', :filename =&gt; tmp_filename.to_s) File.delete tmp_filename } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload