We have list of category products having duplicate names. How to get a list of products which should not have duplicate product name in Postgres?
We are searching for min product ids with group by name. then searching the products in ids.
category = Category.first
ids = Product.select("MIN(id) as id").where(deleted: false).group(:name).collect(&:id)
category.products.where("products.id IN (?)", ids).find_active
How can we optimize the queries?