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…
Star B. 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
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
Mohit S. wrote:
Star B. 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
Cheers
Mohit.
Hmmm. I have the following model:
class Entry < ActiveRecord::Base
has_and_belongs_to_many :products
file_column :image, :magick => {
:versions => { “thumb” => “100x100”, “medium” => “640x480” }
}
end
So entry (which represents an image) has a file column in my
understanding. My controller method to delete an entry looks like this:
def delete
@entry = Entry.find(params[:id])
@entry.products.clear
Entry.delete(params[:id])
jump_back
end
This code does delete an entry and the relations to products but DOES
NOT affect the files - i.e. the files (in my case several files per
uploaded image) are remaining…
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.
Star B. wrote:
}
I should have bolded the word destroy in my reply
Cheers
Mohit.