Remove duplicate records based on multiple columns?

前端 未结 7 2061
灰色年华
灰色年华 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:00

    To run it on a migration I ended up doing like the following (based on the answer above by @aditya-sanghi)

    class AddUniqueIndexToXYZ < ActiveRecord::Migration
      def change
        # delete duplicates
        dedupe(XYZ, 'name', 'type')
    
        add_index :xyz, [:name, :type], unique: true
      end
    
      def dedupe(model, *key_attrs)
        model.select(key_attrs).group(key_attrs).having('count(*) > 1').each { |duplicates|
          dup_rows = model.where(duplicates.attributes.slice(key_attrs)).to_a
          # the first one we want to keep right?
          dup_rows.shift
    
          dup_rows.each{ |double| double.destroy } # duplicates can now be destroyed
        }
      end
    end
    

提交回复
热议问题