<% 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:
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.
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
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: