I have implemented a safe destroy method for our User model since users can only be hard destroyed unless the user has surveys, otherwise, a soft destroy (by setting deleted_at
) is done:
alias_method :original_destroy, :destroy
def destroy
if surveys.any?
update_column :deleted_at, DateTime.now
surveys.each { |s| s.anonymize }
send :_run_destroy_callbacks
@destroyed = true
freeze
else
original_destroy
end
end
I'm not sure whether there's a better way to destroy all associationes with dependent: destroy
than the send :_run_destroy_callbacks
line above.
Thanks for your hints!