907cfbb1f96a3ff5f1c9dcaa28861dfc

In my rails application an Area has_many Videos.

I wrote a (part of a) view that lists all the areas, and for each area shows the count
of the videos that belong to it.

My code works, but makes a different database query for each Area, which I suppose
is not terribly efficient -- do you know of any way to improve it? Thanks!

@areas = Area.all
<ul>
  <% @areas.each do |area| %>
    <li><span class="titoloAree"><%=h area.name%></span> (<%=h area.videos.count%>)</li>
  <%end%>
</ul>

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

August 25, 2009, August 25, 2009 14:22, permalink

No rating. Login to rate!
@areas = Area.all(:include => :videos)
5e6ceef905d14ade228ea22c445d57bc

Aidan

September 3, 2009, September 03, 2009 06:24, permalink

No rating. Login to rate!

Your refactoring





Format Copy from initial code

or Cancel