Delete files with file-column?

Star Burger wrote:

In the web app I'm currently working on users can upload multiple images - and can also delete them later on.

Is there a way to easily delete uploaded files in file-column? The deletion should affect the database and the file structure of course.

I didn't find any documentation for that...

I find the easiest way to do that is to have a main record that has_one or has_many images. Each image has an ID, parent_id and file_column field. Then, all you need to do is destroy any image record. I believe that takes care of the rest :slight_smile:

Cheers
Mohit.

you can use AR's before_destroy Callback to write a function that reads
the filenname/path from the column and deletes the files from the Disk
...

class Entry < ActiveRecord::Base
  has_and_belongs_to_many :products
  file_column :image, :magick => {
            :versions => { "thumb" => "100x100", "medium" => "640x480"
}
            }
  before_destroy :delete_img_file

protected
def delete_img_file
....some code to delete the image here ...
.... self.file_column should give you the filename/path (however thats
stored in there, dont know the plugin you used)....
end

end

Star Burger wrote:

Thorsten L wrote:
  

you can use AR's before_destroy Callback to write a function that reads
the filenname/path from the column and deletes the files from the Disk
...

class Entry < ActiveRecord::Base
  has_and_belongs_to_many :products
  file_column :image, :magick => {
            :versions => { "thumb" => "100x100", "medium" => "640x480"
}
            }
  before_destroy :delete_img_file

protected
def delete_img_file
....some code to delete the image here ...
.... self.file_column should give you the filename/path (however thats
stored in there, dont know the plugin you used)....
end

end
    
The reason why file column didn't delete the files automatically when I deleted an entry was because I used entry.delete instead of entry.destroy. The latter one kills all uploaded files also...

Thanks for your help.

I should have bolded the word *destroy* in my reply :slight_smile:

Cheers
Mohit.