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

<p>Something like:</p>

<pre><code>Record.find(:all).date.unique.count 
</code></pre>

<p>but of course, that doesn't seem to work.</p>

[{'answer_id': 36618, 'author': 'Natalie Weizenbaum', 'author_id': 2518, 'author_profile': 'https://Stackoverflow.com/users/2518', 'pm_score': 7, 'selected': True, 'text': "<p>What you're going for is the following SQL:</p>\n\n<pre><code>SELECT COUNT(DISTINCT date) FROM records\n</code></pre>\n\n<p>ActiveRecord has this built in:</p>\n\n<pre><code>Record.count('date', :distinct =&gt; true)\n</code></pre>\n"}, {'answer_id': 40293, 'author': 'm104', 'author_id': 4039, 'author_profile': 'https://Stackoverflow.com/users/4039', 'pm_score': 4, 'selected': False, 'text': '<p>Outside of SQL:</p>\n\n<pre><code>Record.find(:all).group_by(&amp;:date).count\n</code></pre>\n\n<p>ActiveSupport\'s <a href="http://api.rubyonrails.com/classes/Enumerable.html#M001795" rel="noreferrer">Enumerable#group_by</a> is indispensable.</p>\n'}, {'answer_id': 71613, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 2, 'selected': False, 'text': "<p>Also, make sure you have an index on the field in your db, or else that query will quickly become sloooow.</p>\n\n<p>(It's much better to do this in SQL, otherwise you pull the entire db table into memory just to answer the count.)</p>\n"}, {'answer_id': 19362288, 'author': 'Yule', 'author_id': 671422, 'author_profile': 'https://Stackoverflow.com/users/671422', 'pm_score': 7, 'selected': False, 'text': "<p>This has changed slightly in rails 4 and above <code>:distinct =&gt; true</code> is now deprecated. Use:</p>\n\n<pre><code>Record.distinct.count('date')\n</code></pre>\n\n<p>Or if you want the date and the number:</p>\n\n<pre><code>Record.group(:date).distinct.count(:date)\n</code></pre>\n"}, {'answer_id': 24914018, 'author': 'leompeters', 'author_id': 2334082, 'author_profile': 'https://Stackoverflow.com/users/2334082', 'pm_score': 3, 'selected': False, 'text': "<p>Detailing the answer:</p>\n\n<pre><code>Post.create(:user_id =&gt; 1, :created_on =&gt; '2010-09-29')\nPost.create(:user_id =&gt; 1, :created_on =&gt; '2010-09-29')\nPost.create(:user_id =&gt; 2, :created_on =&gt; '2010-09-29')\nPost.create(:user_id =&gt; null, :created_on =&gt; '2010-09-29')\n\nPost.group(:created_on).count\n# =&gt; {'2010-09-29' =&gt; 4}\n\nPost.group(:created_on).count(:user_id)\n# =&gt; {'2010-09-29' =&gt; 3}\n\nPost.group(:created_on).count(:user_id, :distinct =&gt; true) # Rails &lt;= 3\nPost.group(:created_on).distinct.count(:user_id) # Rails = 4\n# =&gt; {'2010-09-29' =&gt; 2}\n</code></pre>\n"}, {'answer_id': 33089317, 'author': 'JacobEvelyn', 'author_id': 1103543, 'author_profile': 'https://Stackoverflow.com/users/1103543', 'pm_score': 3, 'selected': False, 'text': '<p>As I mentioned <a href="https://stackoverflow.com/a/33089246/1103543">here</a>, in Rails 4, using <code>(...).uniq.count(:user_id)</code> as mentioned in other answers (for this question and elsewhere on SO) will actually lead to an extra <code>DISTINCT</code> being in the query:</p>\n\n<p><code>SELECT DISTINCT COUNT(DISTINCT user_id) FROM ...</code></p>\n\n<p>What we actually have to do is use a SQL string ourselves:</p>\n\n<p><code>(...).count("DISTINCT user_id")</code></p>\n\n<p>Which gives us:</p>\n\n<p><code>SELECT COUNT(DISTINCT user_id) FROM ...</code></p>\n'}, {'answer_id': 44671200, 'author': 'Yi Feng Xie', 'author_id': 2047546, 'author_profile': 'https://Stackoverflow.com/users/2047546', 'pm_score': 4, 'selected': False, 'text': '<p>the latest <code>#count</code> on rails source code only accept 1 parameter.\nsee: <a href="http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-count" rel="noreferrer">http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-count</a></p>\n\n<p>so I achieved the requirement by</p>\n\n<pre><code>Record.count(\'DISTINCT date\')\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/36608', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3764/']