Hi Pete,
In your case group_by returns a hash where the keys are sections and
the values are arrays of stories, so you need to do something like
this:
in your controller:
@s = Story.find_recent_public_stories
@story_lookup = @s.group_by{ |story| story.section }
in your view:
<% @story_lookup.each do |section, stories| %>
<h2><%= section.name %></h2>
<% for story in stories %>
... do something with story ...
<% end %>
<% end %>
Ryan Bates has a Railcast on group_by: <http://railscasts.com/episodes/
29>
-Brad.
On Jul 10, 12:38 pm, Pete
<pjbek...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi there,
>
> I''m working on my first real rails application (meaning not one
out of
> a book), and I''m not quite sure how to display the results of a
> grouped query. This is what I have in my controller:
>
> @s = Story.find_recent_public_stories
> @stories = @s.group_by {|story| story.section}
>
> In my for loop, I''ve tried various combinations of things like
> @stories.section.name, but so far, I haven''t had any luck.
> @stories.to_xml and @stories.to_yaml works fine, though. Everything
> else returns a NoMethodError.
>
> My ultimate goal is to get a list of stories sorted by category.
>
> Thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---