Sadly, my ruby ignorance has gotten the best of me. I'm trying to turn a locater function into a marketing tool. I generate a list of zip codes (with their distances from the given zip). I pass that list via find_by_all to my sites model and grab all of the sites in that list.

Here's what I want to accomplish:
  • display all sites within 20 to 30 miles - done
  • display preferred sites that may be outside that radius - done
  • count the number of results - I think done
  • if count is less than 2, then display sites from wider radius until I have at least 2 - I'm losing it here


Here's my code ... it works up to the last part:
Code:
<% if @results %>
   Here are the sites we found:
      <table>
      	<tr>
      		<th>Zip</th>
      		<th>Distance</th>
      		<th>Site</th>
      	</tr>
      	<% @sitecount = 1%> # initialize my counter

      	#parse through each zipcode I pulled
        <% @results.each do |zip| %> 

          #for each zipcode, iterate through my list of sites to find a site in that zip
          <% @sites.each do |site| %> 
            <% if site.zip == zip.zip && (zip.distance_to_search_zip('miles') <= 30 || zip.distance_to_search_zip('miles') <= site.referral_radius) %>
              <% @dealercount += 1 %>
              <tr>
                <td><%= zip.zip %></td> 
                <td><%= sprintf("%.2f", zip.distance_to_search_zip('miles')) %> </td> 
                <td><%= site.name %></td>
              </tr>
            <% end %>
            
          <% end %>
          
        <% end %>
        
       #this is what I can't get to work
        <% @results.each do |zip| unless @sitecount >= 2 %>
          <% @sites.each do |site| %>
            <% if site.zip == zip.zip && zip.distance_to_search_zip('miles') > 30 %>
              <% @sitecount += 1 %>
              <tr>
                <td><%= zip.zip %></td> 
                <td><%= sprintf("%.2f", zip.distance_to_search_zip('miles')) %> </td> 
                <td><%= site.name %></td>
              </tr>
            <% end %>
          <% end %>
        <% end %>           
      </table>
<% end %>
I thought that after I went through my initial parsing of the zips, I could check the number of results and parse through it a second time, grabbing sites from a wider radius. My logic here is obvioulsy flawed.

Any suggestions on how I should approach this? (other than moving it to the model/controller/wherever ... I'm trying to get the logic down first ... then I'll recode it where it should be).

Thanks!!!

zaed