New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add group_by
to ActiveRecord::FinderMethods
, take two
#10797
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -638,8 +638,10 @@ will return instead a maximum of 5 clients beginning with the 31st. The SQL look | |
SELECT * FROM clients LIMIT 5 OFFSET 30 | ||
``` | ||
|
||
Group | ||
----- | ||
Grouping | ||
-------- | ||
|
||
### `group` | ||
|
||
To apply a `GROUP BY` clause to the SQL fired by the finder, you can specify the `group` method on the find. | ||
|
||
|
@@ -659,6 +661,25 @@ FROM orders | |
GROUP BY date(created_at) | ||
``` | ||
|
||
### `group_by` | ||
|
||
Similar to [Ruby's `Enumerable#group_by`](http://ruby-doc.org/core/Enumerable.html#method-i-group_by), Active Record's `group_by` method provides a way to retrieve all records that correspond to each distinct value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you wrap this line around 80 chars as well please ? 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the rest of the guide is wrapped, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but we are slowly wrapping the new additions around 80 chars. ;-) But let's wait some more feedback before updating anything. |
||
|
||
For example, to get the list of users with each role: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not consistent with your example ; you should either update the example or this sentence. Actually, I would be a bit more descriptive, saying something like "Given you have a X model with a Y column, you can group ...", what do you think ? |
||
|
||
```ruby | ||
Address.group_by(:state) | ||
# => {"AL" => #<ActiveRecord::Relation [...]>, "AK" => #<ActiveRecord::Relation [...]>, ...} | ||
``` | ||
|
||
Under the hood, it will execute the SQL to retrieve the distinct values of the column: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "it will execute the following SQL query", what do you think ? |
||
|
||
```sql | ||
# SELECT DISTINCT state FROM addresses | ||
``` | ||
|
||
Without loading the full records from the database. | ||
|
||
Having | ||
------ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about using an
HashWithIndifferentAccess
here ?