I would like to set up a particular uploader so that when the related model object is destroyed the file on amazon s3 is not removed.
The reason for this is that my Upload model record although destroyed is still referenced within a second audit table.
I am using fog, carrierwave and s3.
Actually there is a way to do this, you just need to skip callback that deletes it:
skip_callback :commit, :after, :remove_<column_name>!
for example
# user.rb
mount_uploader :avatar
skip_callback :commit, :after, :remove_avatar!
see https://github.com/carrierwaveuploader/carrierwave#skipping-activerecord-callbacks
Well AFAIK remove_previously_stored_files_after_update
only work when a model object is updated
so setting it to false
will not remove the old file
during update
But in your case you have to ensure the file are still present when the related model object is destroy
well I dont think there(if you examine the code here) is any mechanism currently available in in Carrierwave
do that
but you can overwritten the remove!
to achieve the same I guess this involve setting up attr_accessor
(which is flag to decide whether to keep the file or delete it)
Inside your desired Model define a attr_accessor
(say keep_file)
and in the desired uploader override the remove! method
class MyUploader < CarrierWave::Uploader::Base
def remove!
unless model.keep_file
super
end
end
end
and ensure that you set the attr_accessor
for the object (if you want to keep the deleted file) before destroying them
Example
u = User.find(10)
u.keep_file = true
u.destroy
This will ensure that file are cleaned up when the record is deleted from database
Let me know if there any better to do this
Hope this help
Keeping files for all, or some uploaders
CarrierWave.configure do |config|
config.remove_previously_stored_files_after_update = false
end
If you want to configure that on a per-uploaded basis:
class AvatarUploader < CarrierWave::Uploader::Base
configure do |config|
config.remove_previously_stored_files_after_update = false
end
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With