Created
October 29, 2012 14:45
-
-
Save dascgo/3973944 to your computer and use it in GitHub Desktop.
remove duplicate imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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