How can I count the number of records that have a unique value in a particular field in ROR?

前端 未结 7 1045
予麋鹿
予麋鹿 2020-12-04 21:21

I have a record set that includes a date field, and want to determine how many unique dates are represented in the record set.

Something like:

Record         


        
相关标签:
7条回答
  • 2020-12-04 22:10

    This has changed slightly in rails 4 and above :distinct => true is now deprecated. Use:

    Record.distinct.count('date')
    

    Or if you want the date and the number:

    Record.group(:date).distinct.count(:date)
    
    0 讨论(0)
提交回复
热议问题