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

前端 未结 7 1059
予麋鹿
予麋鹿 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条回答
  •  -上瘾入骨i
    2020-12-04 21:52

    Outside of SQL:

    Record.find(:all).group_by(&:date).count
    

    ActiveSupport's Enumerable#group_by is indispensable.

提交回复
热议问题