开发者

Removing “duplicate objects” with same attributes using Array.map

开发者 https://www.devze.com 2022-12-30 02:19 出处:网络
As you can see in the current code below, I am finding the duplicate based on the attribute recordable_id. What I need to do is find the duplicate based on four matching attributes: user_id, recordabl

As you can see in the current code below, I am finding the duplicate based on the attribute recordable_id. What I need to do is find the duplicate based on four matching attributes: user_id, recordable_type, hero_type, recordable_id. How must I modify the code?

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 be开发者_运维问答en seen already
      hero_status.revoke
    else
      seen << hero_status # if not, add it to the seen array
    end
  end
end


Try this:

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

Edit 2 To revoke the all the latest duplicate entries do the following:

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


Using straight Ruby (not the SQL server):

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

For lookups, always use Hash (or Set, but here I thought it would be nice to keep the statuses that have been kept)

Note: I used sort_by!, but that's new to 1.9.2, so use sort_by (or require "backports")

0

精彩评论

暂无评论...
验证码 换一张
取 消