Hi, There is something I don''t understand about :dependent => :destroy. I hope someone can help me. Consider this code: class Folder < ActiveRecord::Base has_many :myfiles has_many :folders has_many :group_folders, :dependent => :destroy validates_uniqueness_of :name, :scope => "folder_id" validates_presence_of :name before_destroy :dont_destroy_folder_with_contents # Folders containing files or sub-folders # can not be deleted def dont_destroy_folder_with_contents if self.folders.length > 0 or self.myfiles.length > 0 raise "Can''t destroy folder with contents" end end end It prevents folders with contents from being deleted, however the group_folders do get deleted because of :dependent => :destroy. How can I prevent the group_folders from being destroyed when a folder "has contents"? I do want the group_folders to be destroyed when the folder is destroyed without a problem. Thanks. Mischa.
Anyone...? Mischa Berger wrote:> Hi, > > There is something I don''t understand about :dependent => :destroy. I > hope someone can help me. Consider this code: > > class Folder < ActiveRecord::Base > has_many :myfiles > has_many :folders > has_many :group_folders, :dependent => :destroy > > before_destroy :dont_destroy_folder_with_contents > # Folders containing files or sub-folders > # can not be deleted > def dont_destroy_folder_with_contents > if self.folders.length > 0 or self.myfiles.length > 0 > raise "Can''t destroy folder with contents" > end > end > end > > It prevents folders with contents from being deleted, however the > group_folders do get deleted because of :dependent => :destroy. How can > I prevent the group_folders from being destroyed when a folder "has > contents"? I do want the group_folders to be destroyed when the folder > is destroyed without a problem. > > Thanks. > > Mischa.
2006/3/12, Mischa Berger <spmm_pls@yahoo.com>:> before_destroy :dont_destroy_folder_with_contents > # Folders containing files or sub-folders > # can not be deleted > def dont_destroy_folder_with_contents > if self.folders.length > 0 or self.myfiles.length > 0 > raise "Can''t destroy folder with contents" > end > end > endDid you try returning false from here ? I think that is what you are supposed to do to prevent an action. Hope that helps ! -- Fran?ois Beausoleil http://blog.teksol.info/
> Did you try returning false from here ? I think that is what you are > supposed to do to prevent an action.Yeah, tried that and the group_folders still get deleted...
OK, someone on IRC helped me out. This is the way to do it: class Folder < ActiveRecord::Base has_many :myfiles has_many :folders has_many :group_folders before_destroy :dont_destroy_folder_with_contents # Folders containing files or sub-folders # can not be deleted def dont_destroy_folder_with_contents if self.folders.length > 0 or self.myfiles.length > 0 raise "Can''t destroy folder with contents" end end after_destroy :destroy_dependant_group_folders # Delete dependant group_folders # This code should be executed after_destroy def destroy_dependant_group_folders for group_folder in self.group_folders group_folder.destroy end end end
On 3/13/06, Mischa Berger <spmm_pls@yahoo.com> wrote:> after_destroy :destroy_dependant_group_folders > # Delete dependant group_folders > # This code should be executed after_destroy > def destroy_dependant_group_folders > for group_folder in self.group_folders > group_folder.destroy > end > end > endBut what if one or some of the group_folder.destroy commands fail? You could use a transaction to make sure that this entire destroy is an all or none change to the db. -Peter
> But what if one or some of the group_folder.destroy commands fail? > > You could use a transaction to make sure that this entire destroy is > an all or none change to the db.Thanks for the suggestion, but I don''t think this is neccesary in my situation.