2012-05-02 295 views
0

如果在特定日期有多個記錄,我想刪除當天除最新記錄以外的所有記錄。例如,在ID爲9,10,12的表格記錄中具有相同的日期。所以9和10應該被刪除,因爲ID爲12的記錄具有最新的日期。ActiveRecord:刪除重複記錄

id  date 
1 2012-04-25 00:00:00.000000 
2 2012-04-26 00:00:00.000000 
3 2012-04-23 00:00:00.000000 
4 2012-04-24 00:00:00.000000 
5 2012-05-01 00:00:00.000000 
6 2012-05-02 00:00:00.000000 
7 2012-05-03 00:00:00.000000 
8 2012-05-04 00:00:00.000000 
9 2012-04-30 00:30:00.000000 
10 2012-04-30 18:00:00.000000 
11 2012-04-29 00:00:00.000000 
12 2012-04-30 18:40:00.000000 
13 2012-05-05 00:00:00.000000 
14 2012-05-05 09:31:31.000000 

這裏是(髒)rake任務刪除重複

task :remove_duplicate do 
    Rake::Task["remove_duplicate"].invoke 
end 

task :remove_duplicate => :environment do 
    weights = Weight.count(:group => "DATE(date)", :having => "COUNT(id) > 1") 
    weights_to_delete = [] 
    weights.each do |weight| 

    start_date = weight[0].to_date.beginning_of_day 
    end_date = weight[0].to_date.end_of_day 
    day_weights = Weight.where("date >= ? and date <= ?", start_date, end_date).order(:date) 
    day_weights[0..-2].each do |weight| 
     weights_to_delete.push weight.id 
    end 
    end 
    Weight.delete(weights_to_delete) 
end 

雖然我能爲我解釋刪除的記錄,我不滿意我採取的做法。請指導我刪除特定日期的重複記錄,以保持最新版本只使用ActiveRecord API更好的方式。

謝謝,阿米特·帕特爾

回答

4

這種方法可能是緩慢,除非你正在運行的我不推薦它定期。

Weight.all.each do |weight| 
    Weight.order("id desc").where(date: weight.date).all.drop(1).each { |w| w.delete } 
end 
+0

穩紮穩打,但它做的工作,並作爲一個時間的操作我使用時更喜歡在速度之前閱讀清楚易懂的代碼。 – lime

0

你可以試試這個SQL查詢,在該日期刪除同一日期的記錄,但最新的一個

DELETE FROM weights USING weights weight WHERE (CAST(weights.date as Date) = CAST(weight.date as Date) AND weights.id < weight.id);