Displaying 2 results from an estimated 2 matches for "dont_destroy_folder_with_contents".
2006 Mar 13
6
:dependent => :destroy
...ope 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...
2005 Oct 23
12
Showing a neat error message
Hi All,
I''m trying to prevent users from deleting a folder that has contents
like this:
class Folder < ActiveRecord::Base
has_many :myfiles
has_many :folders
belongs_to :folder
validates_uniqueness_of :name, :scope => "folder_id"
before_destroy :dont_destroy_folder_with_contents
def dont_destroy_folder_with_contents
if Folder.find(id) != nil || Myfile.find(id) != nil
raise "Cannot delete this folder because it has contents"
end
end
end
This works fine: the folder''s not deleted. However I do not get the neat
error message with the red b...