Removing Duplicates

Friday, 2 January 2009

Today i was checking in on one of my applications and i had noticed that i forgot to check for unique user entries. This is bad design on my part for several reasons.

  1. When a user submits a form, the submit button should reflect that the request was sent. This can be accomplished by “greying” out the button or by using some AJAZZ progress indication.
  2. It is not that hard to add
validates_uniqueness_of :whatever

nevertheless, I found myself in a position to delete a gaggle of duplicates. Now, the list was certainly manageable but i prefer to let the machines do my work. I sshd’ myself on over to my server and fired up a script/console production. And this is what i did:

#warning! this script can get you fired. 
 
a = People.find(:all)
b = Hash[ *a.map{ |x| [ x[:social_security_number], x] }.flatten ].values
c = a - b
c.each { |x| x.destroy }

What this script does

#1 => grabs all of the people from the database
#2 => loads up all of the people who have the same social
#3 => pull out the duplicates
#4 => makes ActiveRecord calls to remove duplicates from database


Comments

  1. Was looking exactly for this – thanks! I’m not a Ruby expert and your curly and square braces are not matching, so it’s not working for me and I can’t resolve it myself.

    Posted by Nick | March 22, 2009 5:46 pm
  2. Thanks for the tip! I was missing a “]” Everything should be back up to par.

    Posted by Ryan Smith | March 23, 2009 5:35 pm
Leave a comment
Name(required)
Mail (will not be publishing) (required)
Website