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. -- Posted via http://www.ruby-forum.com/.
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 > > EditorYou 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 -- Posted via http://www.ruby-forum.com/.
Steve Erickson
2006-Apr-09 20:30 UTC
[Rails] Re: Write/Display AR query as Grouped Results?
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 %> -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
Jean-François
2006-Apr-09 22:56 UTC
[Rails] Re: Write/Display AR query as Grouped Results?
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/new-in-rails-enumerable-group_by-and-array-in_groups_of and r3726. -- Jean-Fran?ois. -- ? la renverse.
Jeff Coleman
2006-Apr-09 23:55 UTC
[Rails] Re: Re: Write/Display AR query as Grouped Results?
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/new-in-rails-enumerable-group_by-and-array-in_groups_of > > 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 -- Posted via http://www.ruby-forum.com/.
Jean-François
2006-Apr-10 00:47 UTC
[Rails] Re: Re: Write/Display AR query as Grouped Results?
Hello Jeff,> 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/new-in-rails-enumerable-group_by-and-array-in_groups_of > > > > and r3726. > > > 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. -- ? la renverse.