Forum: Ruby on Rails find :all using :group

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F306ebfb58237d051253fef54dd6f01a?d=identicon&s=25 Ayeye Brazov (brazov)
on 2009-03-23 15:21
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
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2009-03-23 19:25
(Received via mailing list)
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 Brazov <rails-mailing-l...@andreas-s.net>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-23 19:39
(Received via mailing list)
On Mar 23, 2:21 pm, Ayeye Brazov <rails-mailing-l...@andreas-s.net>
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
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-03-23 20:01
(Received via mailing list)
On Mar 23, 11:38 am, Frederick Cheung <frederick.che...@gmail.com>
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'
829f2eef24c244ea33165adde8bd36d5?d=identicon&s=25 Party Drone (Guest)
on 2009-03-23 20:22
(Received via mailing list)
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 Brazov <rails-mailing-l...@andreas-s.net>
F306ebfb58237d051253fef54dd6f01a?d=identicon&s=25 Ayeye Brazov (brazov)
on 2009-03-23 23:26
>> 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.
This topic is locked and can not be replied to.