How can I reduce the number of loops in this VIEW in Rails when using :collection?
Posted
by Angela
on Stack Overflow
See other posts from Stack Overflow
or by Angela
Published on 2010-05-27T03:54:30Z
Indexed on
2010/05/27
4:01 UTC
Read the original article
Hit count: 252
I am using the :collection to go through all the Contacts that are part of a given Campaign.
But within that Campaign I check for three different Models (each with their own partial). Feels like I am going through the list of Contacts 3x. How can I make this alot leaner?
<h2>These are past due:</h2>
<% @campaigns.each do |campaign| %>
<h3>Campaign: <%= link_to campaign.name, campaign %></h3>
<strong>Emails in this Campaign:</strong>
<% for email in campaign.emails %>
<h4><%= link_to email.title, email %> <%= email.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_email",
:collection => @contacts,
:locals => {:email => email} %>
<% end %>
Calls in this Campaign:
<% for call in campaign.calls %>
<h4><%= link_to call.title, call %> <%= call.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_call",
:collection => @contacts,
:locals => {:call => call} %>
<% end %>
Letters in this Campaign:
<% for letter in campaign.letters %>
<h4><%= link_to letter.title, letter %> <%= letter.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_letter",
:collection => @contacts,
:locals => {:letter => letter} %>
<% end %>
<% end %>
© Stack Overflow or respective owner