Remove duplicate records based on multiple columns?

前端 未结 7 2109
灰色年华
灰色年华 2020-12-04 07:46

I\'m using Heroku to host my Ruby on Rails application and for one reason or another, I may have some duplicate rows.

Is there a way to delete duplicate records base

7条回答
  •  醉酒成梦
    2020-12-04 08:14

    class Model
    
      def self.dedupe
        # find all models and group them on keys which should be common
        grouped = all.group_by{|model| [model.name,model.year,model.trim,model.make_id] }
        grouped.values.each do |duplicates|
          # the first one we want to keep right?
          first_one = duplicates.shift # or pop for last one
          # if there are any more left, they are duplicates
          # so delete all of them
          duplicates.each{|double| double.destroy} # duplicates can now be destroyed
        end
      end
    
    end
    
    Model.dedupe
    
    • Find All
    • Group them on keys which you need for uniqueness
    • Loop on the grouped model's values of the hash
    • remove the first value because you want to retain one copy
    • delete the rest

提交回复
热议问题