Forum: Ruby on Rails :dependent => :destroy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mischa B. (Guest)
on 2006-03-13 02:15
(Received via mailing list)
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.
Mischa B. (Guest)
on 2006-03-13 23:20
(Received via mailing list)
Anyone...?
François B. (Guest)
on 2006-03-13 23:50
(Received via mailing list)
2006/3/12, Mischa B. <removed_email_address@domain.invalid>:
>    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

Did you try returning false from here ?  I think that is what you are
supposed to do to prevent an action.

Hope that helps !
Mischa B. (Guest)
on 2006-03-14 00:04
(Received via mailing list)
> 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...
Mischa B. (Guest)
on 2006-03-14 02:02
(Received via mailing list)
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
Peter M. (Guest)
on 2006-03-14 02:09
(Received via mailing list)
On 3/13/06, Mischa B. <removed_email_address@domain.invalid> 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
> end

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.

-Peter
Mischa B. (Guest)
on 2006-03-15 00:45
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.