How to find records that have duplicate data using Active Record

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

    In Rails 2.x, select is a private method of AR class. Just use find():

    klass.find(:all, 
      :select => "id, count(the_col) as num", 
      :conditions => ["extra conditions here"], 
      :group => 'the_col', 
      :having => "num > 1")
    

提交回复
热议问题