Oakland510 Oakland510 - 9 months ago 86
Ruby Question

Deleting files in ruby

I'm using the carrierwave gem to upload files. I have built a system for users to flag images as inappropriate and for admins to remove the images. From what I can tell, calling destroy on the image will only remove the path name from the tabel. Is there a way to have ruby actually remove the file itself? Or should rails be automatically removing the file when I destroy the image path?


Not sure what CarrierWave offers for this, but you could use FileUtils in the Ruby standard library with an ActiveRecord callback.

For instance,

require 'FileUtils'
before_destroy :remove_hard_image

def remove_hard_image

Sidenote: This code is from memory.