Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was to solve this, although I'm still not sure if it's the most elegant solution.</p> <p>First off, I mentioned in my question that when I registered config_for_remote_upload with the before_validation callback, the file was not uploaded to S3, but the image_remote column was populated. Upon further inspection, the situation is even worse. When initializing the image_remote uploader within the before_validation callback, all files were deleted on the S3 storage bucket! I replicated this a couple times. I only tested when the store_dir was set to nil in the uploaded, thus putting the files at the root of the bucket.</p> <p>Initializing the image_remote column in during the before_save callback does not have this problem. In order force the record to save (it wouldn't save, because only a non db column attribute was being changed) I added a before_validation that changed the update_at field of the record.</p> <pre><code>before_validation: :change_record_updated_at ... def change_record_updated_at self.update_at=Time.current end </code></pre> <p>I also moved away from using dup, not because it didn't work, but rather because I didn't know why it worked. Instead I created a StringIO object for the file and assigned that to the image_remote column.</p> <pre><code> def config_for_remote_upload if self.remote.to_i==1 #self.image_remote = self.image.dup #this will open the file as binary img_binary = File.open(self.image.file.path){ |i| i.read } img_encoded = Base64.encode64(img_binary) io = FilelessIO.new(Base64.decode64(img_encoded)) io.original_filename = self.image.file.original_filename self.image_remote = io elsif self.remote.to_i==0 #delete remote image and clear field self.remove_image_remote = true end end </code></pre> <p>See <a href="https://stackoverflow.com/questions/6718841/restful-file-uploads-with-carrierwave">here</a> for further info on FilelessIO (StringIO with original_filename).</p> <p>With this configuration, the file can be uploaded to the second storage location (S3 in my case) after the initial upload.</p> <p>Hope this helps someone else out.</p>
 

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