Forum: Ruby on Rails Write/Display AR query as Grouped Results?

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.
chris (Guest)
on 2006-04-09 18:29
I've got a publications table that contains an author_id foreign key and
a pubrole_id foreign key. What I want to do is query the DB using AR so
that I can get a list of all publications that belong_to a particular
author, and group the results by the pubrole.role_name (Author, Joint
Author, Editor, etc.) so that the results look something like:

Author
  book1 info
  book2 info
  etc.

Joint Author
  book1 info
  book2 info

Editor

etc.

Can anyone suggest how to do this, or point to an example that already
exists?

Thanks.
Jeff C. (Guest)
on 2006-04-09 21:50
chris wrote:
> I've got a publications table that contains an author_id foreign key and
> a pubrole_id foreign key. What I want to do is query the DB using AR so
> that I can get a list of all publications that belong_to a particular
> author, and group the results by the pubrole.role_name (Author, Joint
> Author, Editor, etc.) so that the results look something like:
>
> Author
>   book1 info
>   book2 info
>   etc.
>
> Joint Author
>   book1 info
>   book2 info
>
> Editor

You can construct the sql clause with "find_by_sql":

@publications = Publication.find_by_sql ["select p.*, r.role_name from
publications p, pubroles r where p.author_id = ? and p.pubrole_id = r.id
group by r.role_name, p.book_name", author_id]

...or something like that. :)

Jeff
Steve E. (Guest)
on 2006-04-10 00:30
chris wrote:
> I've got a publications table that contains an author_id foreign key and
> a pubrole_id foreign key. What I want to do is query the DB using AR so
> that I can get a list of all publications that belong_to a particular
> author, and group the results by the pubrole.role_name (Author, Joint
> Author, Editor, etc.) so that the results look something like:
>
> Author
>   book1 info
>   book2 info
>   etc.
>
> Joint Author
>   book1 info
>   book2 info
>
> Editor
>
> etc.
>
> Can anyone suggest how to do this, or point to an example that already
> exists?
>
> Thanks.

Try using the new group_by function.  It works great.  you should be
able to do it using something like the following:

CONTROLLER:
@publications = Publication.find :all, :conditions => ['author_id = ?',
@author.id]

VIEW:
<% @publications.group_by{|p| p.pubrole.role_name }.each do |role_name,
publications| %>


<h2><%= role_name %></h2>

    <% for pub in publications %>
        . . .
    <% end %>

<% end %>
chris (Guest)
on 2006-04-10 02:17
Steve

Is that new for 1.1? Very cool. Any documentation on it? I looked at
api.rubyonrails.com but didnt see it listed there.
Jean-François (Guest)
on 2006-04-10 02:59
(Received via mailing list)
Hi Chris,
> Is that new for 1.1? Very cool. Any documentation on it? I looked at
> api.rubyonrails.com but didnt see it listed there.

See  :
http://weblog.rubyonrails.org/articles/2006/03/01/...

and r3726.

    -- Jean-François.
Jeff C. (Guest)
on 2006-04-10 03:55
Jean-François wrote:
> Hi Chris,
>> Is that new for 1.1? Very cool. Any documentation on it? I looked at
>> api.rubyonrails.com but didnt see it listed there.
>
> See  :
> 
http://weblog.rubyonrails.org/articles/2006/03/01/...
>
> and r3726.
>
>     -- Jean-François.

I noticed that too, but I am also curious about where is the actual
documetation for it?  What module contains groups_by, and is there a
proper RDoc for it?

Jeff
Jean-François (Guest)
on 2006-04-10 04:48
(Received via mailing list)
Hello Jeff,

> I noticed that too, but I am also curious about where is the actual
> documetation for it?

You're right, there isn't :)

> What module contains groups_by, and is there a
> proper RDoc for it?

It's an add-on for the module Enumerable so it's in ActiveSupport, in
activesupport-xx/lib/active_support/core_ext/enumerable.rb

and you will see in the first line of the file :

module Enumerable #:nodoc:

The news or the source are the documentation.

    -- Jean-François.
This topic is locked and can not be replied to.