2013-03-05 106 views
0

这里有很多类似的问题,但找不到我的好答案。使用耙子任务删除重复项

我有EntryVote模型与字段user_id,entry_id和其他一些。

我想创建简单的rake任务来删除重复的user_identry_id组(没关系whic战绩从小组左)。 做这件事的最好方法是什么?

例如:

id, user_id, entry_id 
1,1,1 
2,1,1 
3,1,1 
4,5,6 
5,5,6 
6,7,7 

我得到:

1,1,1 
4,5,6 
6,7,7 

我知道如何选择USER_ID,对于重复数据删除entry_id,但不知道如何使用它以后的工作:

EntryVote.select('user_id, entry_id').group('user_id,entry_id').having('count() > 1')

+0

要确认,你想删除'user_id'和'entry_id'相同的重复'EntryVotes'? – 2013-03-05 11:56:38

+0

nope,只是用例子编辑问题 – 2013-03-05 12:01:16

回答

0

可能不是最好的解决方案,但尝试f或者您可以添加验证来检查user_id和entry_id的唯一性并尝试保存记录。如果记录由于验证而未保存并失败,则只需删除该记录。我敢肯定,这是慢于:)

0

第一个选项如果你想要的列entry_iduser_id是一个独特的外键,它包含一个特殊的SQL删除以下rake任务声明,就可以

task 'delete_duplicates' => :environment do 
    puts "Removing duplicates in table entry_votes" 
    puts "Entries before: #{n1=EntryVote.count}" 
    sql = "delete e1 from entry_votes e1, entry_votes e2 "+ 
      "where (e1.user_id = e2.user_id) and (e1.entry_id = e2.entry_id) "+ 
      "and (e1.id > 12.id);") 
    ActiveRecord::Base.connection.execute(sql); 
    puts "Entries after: #{n2=EntryVote.count}, #{n1-n2} duplicates removed" 
    end 

另请参阅此SO question about duplicates或此文章how to delete duplicates using SQL