Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #15459 from maurogeorge/add-total-grouped-items

Add Total of grouped items to Active Record query interface on guides [ci skip]
  • Loading branch information...
commit 22820f7bbe6dbd897a78ca281e41448d4e582cea 2 parents 02ee081 + 0885506
@zzak zzak authored
Showing with 34 additions and 0 deletions.
  1. +34 −0 guides/source/active_record_querying.md
View
34 guides/source/active_record_querying.md
@@ -659,6 +659,40 @@ FROM orders
GROUP BY date(created_at)
```
+### Total of grouped items
+
+To get the total of grouped items on a single query call `count` after the `group`.
+
+```ruby
+Order.group(:status).count
+# => { 'awaiting_approval' => 7, 'paid' => 12 }
+```
+
+The SQL that would be executed would be something like this:
+
+```sql
+SELECT COUNT (*) AS count_all, status AS status
+FROM "orders"
+GROUP BY status
+```
+
+It is possible to do this count with multiple values, to do this only add the
+other column to `group`.
+
+```ruby
+Order.group(:status, :delivery_method).count
+# => { ['awaiting_approval', 'regular'] => 5, ['awaiting_approval', 'fast'] => 2, ['paid', 'regular'] => 2, ['paid', 'fast'] => 10 }
+```
+
+The SQL that would be executed would be something like this:
+
+```sql
+SELECT COUNT (*) AS count_all, status AS status,
+delivery_method AS delivery_method
+FROM "orders"
+GROUP BY status, delivery_method
+```
+
Having
------
Please sign in to comment.
Something went wrong with that request. Please try again.