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 %><br />
<% @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?
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