Simple efficiency question: repeated iteration in the view

Let’s say I get a list of all places from the database in the
controller:
@places=Place.find(:all, :order=>“category_id”)

And in the view I want to list them under category headings

Now let’s imagine categories is a hash like this
{0=>‘Museum’,1=>‘Gallery’,2=>‘Historic House’}

The only way I can think of doing it is by traversing the whole list
for every category, ie:

<% categories.each do | key,value | %>
<%= value %>

<% @places.each do | place | %>
<%= place.title if place.category_id==key %>
<% end %>
<% end %>

Is there a better way of doing this that doesn’t involve traversing
the entire array so many times?

Thanks.

On Feb 1, 2:01 pm, AJS [email protected] wrote:

for every category, ie:

<% categories.each do | key,value | %>
<%= value %>

<% @places.each do | place | %>
<%= place.title if place.category_id==key %>
<% end %>
<% end %>

Is there a better way of doing this that doesn’t involve traversing
the entire array
two ideas are:

  1. execute 1 find for each category in the controller and pass the
    results to the view as separate collections. Can’t be sure of the
    relative perfomance though.

  2. use a CASE or if/else if block in the view to presort the places
    into Collections on singlepass then render each collection under the
    apporopriate category heading. This should be faster then traversing
    the array multiple times but may clutter up you view logic

Brian

On Feb 1, 2008 2:01 PM, AJS [email protected] wrote:

The only way I can think of doing it is by traversing the whole list
the entire array so many times?
use #group_by. It takes your array of @places and creates a structure
like this:

{0 => [, ], 1 => [], …}

@places=Place.find(:all, :order=>“category_id”)
@category_places = @places.group_by { |p| p.category_id }
@categories = {0=>‘Museum’,1=>‘Gallery’,2=>‘Historic House’}

@categories.each do |category_id, category|
puts category
@category_places[category_id].each do |place|
puts place
end
end


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

Rick - brilliant. Thanks.