Ruby-on-rails 如何计算 ROR 中特定字段中具有唯一值的记录数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:04:01  来源:igfitidea点击:

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

ruby-on-railsrubyactiverecord

提问by Brent

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.find(:all).date.unique.count 

but of course, that doesn't seem to work.

但当然,这似乎行不通。

回答by Yule

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

这在 rails 4 和更高版本中略有变化,:distinct => true现在已弃用。用:

Record.distinct.count('date')

Or if you want the date and the number:

或者,如果您想要日期和数字:

Record.group(:date).distinct.count(:date)

回答by Natalie Weizenbaum

What you're going for is the following SQL:

你要的是以下SQL:

SELECT COUNT(DISTINCT date) FROM records

ActiveRecord has this built in:

ActiveRecord 有这个内置:

Record.count('date', :distinct => true)

回答by m104

Outside of SQL:

在 SQL 之外:

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

ActiveSupport's Enumerable#group_byis indispensable.

ActiveSupport 的Enumerable#group_by是必不可少的。

回答by Yi Feng Xie

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

最新#count的 Rails 源代码只接受 1 个参数。请参阅:http: //api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-count

so I achieved the requirement by

所以我达到了要求

Record.count('DISTINCT date')

回答by leompeters

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}

回答by JacobEvelyn

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 DISTINCTbeing in the query:

正如我在这里提到的,在 Rails 4 中,使用(...).uniq.count(:user_id)其他答案中提到的(对于这个问题和 SO 上的其他地方)实际上会导致DISTINCT查询中的额外内容:

SELECT DISTINCT COUNT(DISTINCT user_id) FROM ...

SELECT DISTINCT COUNT(DISTINCT user_id) FROM ...

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

我们实际上需要做的是自己使用 SQL 字符串:

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

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

Which gives us:

这给了我们:

SELECT COUNT(DISTINCT user_id) FROM ...

SELECT COUNT(DISTINCT user_id) FROM ...

回答by 0124816

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.)

(最好在 SQL 中执行此操作,否则您将整个 db 表拉入内存只是为了回答计数。)