正如您在下面的當前代碼中所看到的,我發現基於屬性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
我想擺脫最近created_at日期的副本,留下最初創建的記錄。 – keruilin 2010-05-08 01:35:08