Returns false from the before_destroy method to prevent the destruction of the instance.
The method should also return a meaningful error for troubleshooting.
class Project < ActiveRecord::Base
before_destroy :check_tasks
def check_tasks
if self.tasks.any?
errors.add_to_base "Project has tasks and cannot be destroyed."
return false
end
end
end
Note: flash [: notice] and params [: attr_name] can only be used from controllers.