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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 :) Cheers Mohit. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Mohit Sindhwani wrote:> 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 :) > > 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.... :-( -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 > > endThe 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 :-) Cheers Mohit. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---