2010-05-08 114 views
1

正如您在下面的當前代碼中所看到的,我發現基於屬性recordable_id的重複項。我需要做的是根據四個匹配屬性找到重複項:user_id,recordable_type,hero_type,recordable_id。我該如何修改代碼?使用Array.map去除具有相同屬性的「重複對象」

heroes = User.heroes 

for hero in heroes 
    hero_statuses = hero.hero_statuses 

    seen = [] 

    hero_statuses.sort! {|a,b| a.created_at <=> b.created_at } # sort by created_at 
    hero_statuses.each do |hero_status| 
    if seen.map(&:recordable_id).include? hero_status.recordable_id # check if the id has been seen already 
     hero_status.revoke 
    else 
     seen << hero_status # if not, add it to the seen array 
    end 
    end 
end 
+0

我想擺脫最近created_at日期的副本,留下最初創建的記錄。 – keruilin 2010-05-08 01:35:08

回答

0

使用直紅寶石(不是SQL服務器) :

heroes = User.heroes 

for hero in heroes 
    hero_statuses = hero.hero_statuses 

    seen = {} 

    hero_statuses.sort_by!(&:created_at) 
    hero_statuses.each do |status| 
    key = [status.user_id, status.recordable_type, status.hero_type, status.recordable_id] 
    if seen.has_key?(key) 
     status.revoke 
    else 
     seen[key] = status # if not, add it to the seen array 
    end 
    end 

    remaining = seen.values 
end 

進行查找,始終使用Hash(或Set,但在這裏,我認爲這將是很好的保持已保持狀態)

注:我用sort_by!,但這是新1.9.2,所以在使用sort_by(或require "backports"

1

試試這個:

HeroStatus.all(:group => "user_id, recordable_type, hero_type, recordable_id", 
       :having => "count(*) > 1").each do |status| 
    status.revoke 
end 

編輯2 撤銷所有最新的重複項執行以下操作:

HeroStatus.all(:joins => "(
    SELECT user_id, recordable_type, hero_type, 
       recordable_id, MIN(created_at) AS created_at 
    FROM  hero_statuses 
    GROUP BY user_id, recordable_type, hero_type, recordable_id 
    HAVING COUNT(*) > 1 
    ) AS A ON A.user_id   = hero_statuses.user_id   AND 
      A.recordable_type = hero_statuses.recordable_type AND 
      A.hero_type  = hero_statuses.hero_type  AND 
      A.recordable_id = hero_statuses.recordable_id AND 
      A.created_at  < hero_statuses.created_ 
").each do |status| 
    status.revoke 
end 
+0

不錯。這讓我更加接近。這是我忘了提到的另一個皺紋。我想擺脫最近created_at日期的重複項,留下最初創建的記錄。 – keruilin 2010-05-08 01:34:41

+0

更新了答案,看看。 – 2010-05-08 04:44:08

+0

thx kan!這很好。我正在尋找基於ruby的答案,所以要檢查marc的。但感謝您的協助! – keruilin 2010-05-08 09:29:09

相關問題