# Find :all using :group

**URL:** <https://www.ruby-forum.com/t/find-all-using-group/163646>\
**Category:** Rails\
**Created:** [March 23, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646 "2009-03-23T15:21:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 23, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646/1 "2009-03-23T15:21:42Z")

</div>

hello,

I need to group Users depending on their operating systems field “os”,  
so I tried

User.find(:all, :group =\> :os)

however, the result is an array having only one user for each os group.  
I’d like to have ALL the users grouped by :os though

One solution is to write

User.find(:all).group\_by{|u| u.os}

However, I wonder if it could be done within the “find” method ?

thanks

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 23, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646/2 "2009-03-23T19:25:08Z")

</div>

This sounds right to me… find the SQL it generated in your .log and  
execute it in a mysql console… is the output the same?

On Mar 23, 10:21 am, Ayeye B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 23, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646/3 "2009-03-23T19:39:48Z")

</div>

On Mar 23, 2:21 pm, Ayeye B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> User.find(:all, :group =\> :os)
> 
> however, the result is an array having only one user for each os group.  
> I’d like to have ALL the users grouped by :os though
> 
> One solution is to write
> 
> User.find(:all).group\_by{|u| u.os}
> 
> However, I wonder if it could be done within the “find” method ?

Not really. By definition if you group using the database then you  
only get back one row for each value of the grouped column - that’s  
just what group does in database and you want something else.

Fred

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 23, 2009, 8:01pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646/4 "2009-03-23T20:01:42Z")

</div>

On Mar 23, 11:38 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > User.find(:all).group\_by{|u| u.os}
> 
> > However, I wonder if it could be done within the “find” method ?
> 
> Not really. By definition if you group using the database then you  
> only get back one row for each value of the grouped column - that’s  
> just what group does in database and you want something else.

…such as :order =\> ‘os ASC’

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 23, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646/5 "2009-03-23T20:22:17Z")

</div>

User.find(:all, :order =\> ‘os’)

:order sorts ascending by default. You can define the sort order  
explicitly with ASC or DESC (i.e., ‘os ASC’ or ‘os DESC’).

On Mar 23, 8:21 am, Ayeye B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brazov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@brazov](https://www.ruby-forum.com/u/brazov)\
**Post date:** [March 23, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/find-all-using-group/163646/6 "2009-03-23T23:26:49Z")

</div>

> > However, I wonder if it could be done within the “find” method ?
> 
> Not really. By definition if you group using the database then you  
> only get back one row for each value of the grouped column - that’s  
> just what group does in database and you want something else.
> 
> Fred

I see, thanks for that Fred.

Thanks all for the replies  
a.
