How to find records that have duplicate data using Active Record

后端 未结 6 1365
刺人心
刺人心 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

    Translating @TuteC into ActiveRecord:

    sql = 'SELECT id, 
             COUNT(id) as quantity 
             FROM types 
             GROUP BY name 
           HAVING quantity > 1'
    #=>
    Type.select("id, count(id) as quantity")
      .group(:name)
      .having("quantity > 1")
    

提交回复
热议问题