0

Let me start by saying I'm open to a better way to do this, but here's what I have so far.

Lets say I have a zip file with 100 images in it. I want to loop through the zip file and 'attach' each image to a record. I have Paperclip installed to attach the image to the record in ActiveRecord. I'm using the following code so far:

Zip::File.open(params['images'].path) do |zipfile|
  zipfile.each do |file|
      # THIS IS WHAT I'M MISSING
  end
end

This is what I'd like to end up with:

Zip::File.open(params['images'].path) do |zipfile|
  zipfile.each do |file|
      MyModel.create(parent_id: 1, image: "...")
  end
end

How could I do that?

jmcharnes
  • 707
  • 12
  • 23
  • See if this might help: http://stackoverflow.com/questions/4680265/paperclip-how-to-store-a-picture-in-a-rails-console – Taryn East Jul 15 '14 at 01:40

1 Answers1

0

you should create tempfile from zip, than create paperclip attachment with this tempfile

Zip::File.open(params['images'].path) do |zipfile|
    zipfile.each do |file|
        tempfile = Tempfile.new(File.basename(file.name))
        tempfile.binmode
        tempfile.write file.get_input_stream.read

        # to save original file name to model, use image_file_name
        MyModel.create(parent_id: 1, image: tempfile, image_file_name: file.name)

        tempfile.close
    end
end
Attenzione
  • 833
  • 9
  • 12