Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created September 2, 2014 19:49
Show Gist options
  • Save davidbalbert/bebd2ec2bc759b643b60 to your computer and use it in GitHub Desktop.
Save davidbalbert/bebd2ec2bc759b643b60 to your computer and use it in GitHub Desktop.
Community: Destroy duplicate visited statuses
# Returns the number of user+thread combinations with more than one visited
# status, this should be greater than zero before the fix and zero after the
# fix.
VisitedStatus.group([:user_id, :thread_id]).having("COUNT(*) > 1").count.size
# The number of user+thread combinations with at least one visited status. This
# number should remain the same before and after the fix.
VisitedStatus.group([:user_id, :thread_id]).count.size
# The fix: Group all visited statuses by user+thread, select the visited statuses.
VisitedStatus.all.group_by { |x| [x.user_id, x.thread_id] }.lazy.select { |_, vss| vss.size > 1 }.each do |_, vss|
vss.sort { |a, b| b.last_post_number_read <=> a.last_post_number_read}.drop(1).each { |vs| vs.destroy }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment