# Delete files with file-column?

**URL:** <https://www.ruby-forum.com/t/delete-files-with-file-column/72768>\
**Category:** Rails\
**Created:** [October 18, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768 "2006-10-18T19:35:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [October 18, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768/1 "2006-10-18T19:35:05Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [October 18, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768/2 "2006-10-18T19:48:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [October 18, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768/3 "2006-10-18T22:05:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [October 18, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768/4 "2006-10-18T21:50:02Z")

</div>

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… ☹

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [October 19, 2006, 10:01am UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768/5 "2006-10-19T10:01:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![starburger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@starburger](https://www.ruby-forum.com/u/starburger)\
**Post date:** [October 20, 2006, 8:19am UTC](https://www.ruby-forum.com/t/delete-files-with-file-column/72768/6 "2006-10-20T08:19:10Z")

</div>

Star B. wrote:

> > }

I should have bolded the word _destroy_ in my reply 🙂

Cheers  
Mohit.
