Skip to content

Instantly share code, notes, and snippets.

@jackyvo
Created April 3, 2022 14:52
Show Gist options
  • Save jackyvo/ed5fb5a2ac88cfd2cce46558214ec5dc to your computer and use it in GitHub Desktop.
Save jackyvo/ed5fb5a2ac88cfd2cce46558214ec5dc to your computer and use it in GitHub Desktop.
User destroy
#######################################################################
failed_list = []
user_ids_with_enrollment = Enrollment.pluck(:user_id)
users_without_enrollment = User.where.not(id: user_ids_with_enrollment)
users_without_enrollment.find_each do |user|
failed_list << user.id unless user.destroy
end
p "Total users to delete: #{user_without_enrollment.length}"
p "Total users that not deleted: #{failed_list.length}"
#########################################
# Debug failed reason from list of failed
failed_list.each do |user_id|
begin
user = User.find_by(id: user_id)
user.destroy!
rescue Exception => ex
p "#{user_id}: => #{ex.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment