Deleting a previously uploaded Carrierwave file causes new files to be processed

I am using with AWS3 to upload my app file to Amazon. I have an Employee model with an image column (think of it as an employee profile picture) that has a loader installed:

class Employee < ActiveRecord::Base

  mount_uploader :image, ProfileImageUploader




When an employee updates their profile picture, I want the previous one to be deleted. For this, I have the following after_update callback in my Employee model:

class Employee < ActiveRecord::Base


  after_update :remove_changed_image, :if => 'self.image_changed?'

  def remove_changed_image



This successfully deletes the previous file. But I also process the photos that are uploaded. In my Sealer, I have the following:

class ProfileImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_limit => [300, 300]




The problem is that new files are not processed at all. Only one version, unprocessed, is loaded, and if I don't delete the previous image, then everything works as it should (many versions are loaded).

Any help? Thank!


source to share

1 answer

The problem is with the callback after_update

. It is called after the object has been saved, thereby deleting the newly attached file. You must call @employee.remove_image

before saving the object.



All Articles