How to find records that have duplicate data using Active Record

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

    I was beating my head against this problem with a 2016 stack (Rails 4.2, Ruby 2.2), and got what I wanted with this:

    > Model.select([:thing]).group(:thing).having("count(thing) > 1").all.size
     => {"name1"=>5, "name2"=>4, "name3"=>3, "name4"=>2, "name5"=>2}
    

提交回复
热议问题