Remove duplicate records based on multiple columns?

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

    If your User table data like below

    User.all =>
    [
        #, 
        #, 
        #, 
        #, 
        #, 
        #] 
    1.9.2p290 :099 > 
    

    Email id's are duplicate, so our aim is to remove all duplicate email ids from user table.

    Step 1:

    To get all distinct email records id.

    ids = User.select("MIN(id) as id").group(:email,:name).collect(&:id)
    => [15, 16, 18, 19, 17]
    

    Step 2:

    To remove duplicate id's from user table with distinct email records id.

    Now the ids array holds the following ids.

    [15, 16, 18, 19, 17]
    User.where("id NOT IN (?)",ids)  # To get all duplicate records
    User.where("id NOT IN (?)",ids).destroy_all
    

    ** RAILS 4 **

    ActiveRecord 4 introduces the .not method which allows you to write the following in Step 2:

    User.where.not(id: ids).destroy_all
    

提交回复
热议问题