How to find records that have duplicate data using Active Record

后端 未结 6 1265
刺人心
刺人心 2021-01-31 04:12

What is the best way to find records with duplicate values in a column using ruby and the new Activerecord?

6条回答
  •  佛祖请我去吃肉
    2021-01-31 04:32

    With custom SQL, this finds types with same values for name:

    sql = 'SELECT id, COUNT(id) as quantity FROM types
             GROUP BY name HAVING quantity > 1'
    repeated = ActiveRecord::Base.connection.execute(sql)
    

提交回复
热议问题