Skip to content

Instantly share code, notes, and snippets.

@harssh
Forked from rob-murray/find_dups.rb
Created July 5, 2022 22:29
Show Gist options
  • Save harssh/2e50390f9530844f479e2539f916e0ac to your computer and use it in GitHub Desktop.
Save harssh/2e50390f9530844f479e2539f916e0ac to your computer and use it in GitHub Desktop.
Rails find duplicate records
columns_that_make_record_distinct = [:some_id, :another_name]
distinct_ids = Model.select("MIN(id) as id").group(columns_that_make_record_distinct).map(&:id)
duplicate_records = Model.where.not(id: distinct_ids)
@harssh
Copy link
Author

harssh commented Jul 5, 2022

Model.select(:unique_id).group(:unique_id).having("count(*) > 1").size

@harssh
Copy link
Author

harssh commented Jul 5, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment