# Simple efficiency question: repeated iteration in the view

**URL:** <https://www.ruby-forum.com/t/simple-efficiency-question-repeated-iteration-in-the-view/127868>\
**Category:** Rails\
**Created:** [February 1, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/simple-efficiency-question-repeated-iteration-in-the-view/127868 "2008-02-01T23:02:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [February 1, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/simple-efficiency-question-repeated-iteration-in-the-view/127868/1 "2008-02-01T23:02:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [February 3, 2008, 4:45am UTC](https://www.ruby-forum.com/t/simple-efficiency-question-repeated-iteration-in-the-view/127868/2 "2008-02-03T04:45:46Z")

</div>

On Feb 1, 2:01 pm, AJS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [February 3, 2008, 5:56am UTC](https://www.ruby-forum.com/t/simple-efficiency-question-repeated-iteration-in-the-view/127868/3 "2008-02-03T05:56:42Z")

</div>

On Feb 1, 2008 2:01 PM, AJS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![ajs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@ajs](https://www.ruby-forum.com/u/ajs)\
**Post date:** [February 6, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/simple-efficiency-question-repeated-iteration-in-the-view/127868/4 "2008-02-06T15:43:45Z")

</div>

Rick - brilliant. Thanks.
