The easiest way to fix a join table with duplicate entries
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
You're models probably look something like this:
class Model < ApplicationRecord
class OtherModel < ApplicationRecord