0

Is there a that I can make this algorithm faster?

I'm fetching all documents for this Model (Artifact) and for each document I would like to know if there is duplicate and if so I delete it.

   Artifact.all.to_a.each do |n|
      image = n.image_original
      thumb = n.image_thumbnail
      count_value =  Artifact.where(:image_original => image, 
                         :image_thumbnail => thumb).all.to_a.count
      if count_value > 1
        n.destroy!
      end
    end
Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101

1 Answers1

1

You need to call the uniq method on Artifacts collection like this:

@all_artifacts = Artifact.all
@distinct_artifacts = @all_artifacts.uniq {|artifact| artifact.image_original}

You can use Criteria#distinct like this Artifact.distinct(:image_original), but this will only return distinct values with only that field.

Sharvy Ahmed
  • 7,247
  • 1
  • 33
  • 46
  • thanks @Sharvy, I modify to include 2 fields `@distinct_artifacts = @all_artifacts.uniq {|artifact| artifact.image_original && artifact.image_thumbnail}@distinct_artifacts.count` =>`55905` instead of `64806` – Papouche Guinslyzinho Feb 22 '15 at 11:24