Remove duplicate records based on multiple columns?

前端 未结 7 2084
灰色年华
灰色年华 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 07:59

    Based on @aditya-sanghi's answer, with a more efficient way to find duplicates using SQL.

    Add this to your ApplicationRecord to be able to deduplicate any model:

    class ApplicationRecord < ActiveRecord::Base
      # …
    
      def self.destroy_duplicates_by(*columns)
        groups = select(columns).group(columns).having(Arel.star.count.gt(1))
        groups.each do |duplicates|
          records = where(duplicates.attributes.symbolize_keys.slice(*columns))
          records.offset(1).destroy_all
        end
      end
    end
    

    You can then call destroy_duplicates_by to destroy all records (except the first) that have the same values for the given columns. For example:

    Model.destroy_duplicates_by(:name, :year, :trim, :make_id)
    

提交回复
热议问题