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

前端 未结 7 1044
予麋鹿
予麋鹿 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 21:52

    Outside of SQL:

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

    ActiveSupport's Enumerable#group_by is indispensable.

    0 讨论(0)
  • 2020-12-04 21:53

    What you're going for is the following SQL:

    SELECT COUNT(DISTINCT date) FROM records
    

    ActiveRecord has this built in:

    Record.count('date', :distinct => true)
    
    0 讨论(0)
  • 2020-12-04 21:53

    Also, make sure you have an index on the field in your db, or else that query will quickly become sloooow.

    (It's much better to do this in SQL, otherwise you pull the entire db table into memory just to answer the count.)

    0 讨论(0)
  • 2020-12-04 21:58

    the latest #count on rails source code only accept 1 parameter. see: http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-count

    so I achieved the requirement by

    Record.count('DISTINCT date')
    
    0 讨论(0)
  • 2020-12-04 22:04

    As I mentioned here, in Rails 4, using (...).uniq.count(:user_id) as mentioned in other answers (for this question and elsewhere on SO) will actually lead to an extra DISTINCT being in the query:

    SELECT DISTINCT COUNT(DISTINCT user_id) FROM ...

    What we actually have to do is use a SQL string ourselves:

    (...).count("DISTINCT user_id")

    Which gives us:

    SELECT COUNT(DISTINCT user_id) FROM ...

    0 讨论(0)
  • 2020-12-04 22:06

    Detailing the answer:

    Post.create(:user_id => 1, :created_on => '2010-09-29')
    Post.create(:user_id => 1, :created_on => '2010-09-29')
    Post.create(:user_id => 2, :created_on => '2010-09-29')
    Post.create(:user_id => null, :created_on => '2010-09-29')
    
    Post.group(:created_on).count
    # => {'2010-09-29' => 4}
    
    Post.group(:created_on).count(:user_id)
    # => {'2010-09-29' => 3}
    
    Post.group(:created_on).count(:user_id, :distinct => true) # Rails <= 3
    Post.group(:created_on).distinct.count(:user_id) # Rails = 4
    # => {'2010-09-29' => 2}
    
    0 讨论(0)
提交回复
热议问题