In our system we run hourly imports from an external database. Due to an error in the import scripts, there are now some duplicate records.
A duplicate is deemed where a
You can try the following approach:
Product.where.not( id: Product.group(:legacy_id, :company).pluck('min(products.id)') ).delete_all
Or pure sql:
delete from products where id not in ( select min(p.id) from products p group by p.legacy_id, p.company )