Skip to content

Instantly share code, notes, and snippets.

@dascgo
Created October 29, 2012 14:45
Show Gist options
  • Save dascgo/3973944 to your computer and use it in GitHub Desktop.
Save dascgo/3973944 to your computer and use it in GitHub Desktop.
remove duplicate imports
# sql select you were using
medias = Media.find_by_sql("SELECT user_id, name, comment, COUNT(medias.id) FROM medias INNER JOIN users ON medias.user_id = users.id WHERE comment <> "" AND upload_source IN ('Facebook Importer', 'Twitter Importer') GROUP BY comment HAVING COUNT(medias.id) > 1 ORDER BY COUNT(medias.id) DESC;")
# loop through all records found
medias.each do |media|
# get the duplicate records for a particular user
uploads = Media.find(:all, :conditions => {:user_id => media["user_id"], :comment => media["comment"]})
# loop through them
uploads.each_with_index do |upload, index|
# is this the first one?
if index == 0
# reactivate if earlier clean up attempts deactivated.
media.upload_attributes({:active => 1})
# move on
next
end
# deactivate the rest
media.upload_attributes({:active => 0})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment