Skip to content

Instantly share code, notes, and snippets.

@jamiew
Created October 31, 2008 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiew/21325 to your computer and use it in GitHub Desktop.
Save jamiew/21325 to your computer and use it in GitHub Desktop.
def delete_dups_for(model, collect_by)
keep_array = Hash.new { |h,k| h[k] = [] }
delete_array = []
model_name = model.name
all_objects = model.all.reverse #so we add newest first, sort of
pbar = ProgressBar.new(model_name.pluralize, all_objects.count)
all_objects.each do |obj|
if detect_dup((keep_array[obj.send(collect_by)]), obj).nil?
keep_array[obj.send(collect_by)] << obj
else
delete_array << obj.id
end
pbar.inc
end
sql = ActiveRecord::Base.connection();
sql.execute "DELETE FROM #{model.table_name} WHERE id IN(#{delete_array.join(',')})" unless delete_array.empty?
pbar.finish
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment