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?
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]
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:
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?