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
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.