ruby-on-railsdragonfly-gem

Duplicate images uploaded with Dragonfly


I would like to copy a record that has_many :pictures. Copying the record is a no brainer, but copying the Picture records is something else.

The image is stored on an AWS S3 server. From the server perspective I think this does

Thx


Solution

  • I have found how this is done. For people who might ever need this:

    My original Picture object:

    #<Picture:0x007f82570f8f58> {
                :id => 285,
         :image_uid => "2016/10/06/6tacpx09uq_large_0.jpeg",
            :number => nil,
              :main => true,
        :created_at => Thu, 06 Oct 2016 08:59:44 UTC +00:00,
        :updated_at => Thu, 06 Oct 2016 08:59:48 UTC +00:00,
           :user_id => 46,
        :company_id => 27,
            :public => true
    }
    

    Duplicating this is actually not that hard. I used the .dup method provided by Ruby. Copying multiple pictures:

    pictures.each do |p|
      p2 = Picture.create(image:p.image, user:to_user, company:to_company, public:true, main: p.main)
    end
    

    The image:p.image is where you do the actual image duplication.