I recently create some join tables I blissfully let a script run to populate them. The script ran for a few hours and it added about 20k entries which seemed reasonable. Only after it finished did I realize I had failed to create unique constraints on the join table so there were tons of duplicates. So here’s how you can remove duplicates from a join table in rails after the fact.

Let’s say you have two models, “model” and “other_model” and you want to remove duplicates from their join table. The easiest thing to do is:

Model.all.each do |model|
    model.other_model = model.other_model.uniq
end

Yup it’s that easy (:

You’re models probably look something like this:

class Model < ApplicationRecord
  has_and_belongs_to_many :other_model
end

class OtherModel < ApplicationRecord
  has_and_belongs_to_many :model
end