Topic: Better way to itterate through array

Hi, all.

Just wondering if there is a better dry way to itterate through array. Here is what I'm trying to accomplish.

Some history:
I have an inventory program, that I'm writing to learn rails. It has multiple tables, one of the main ones is called parts. Parts has name, manufacturer, vendor, etc columns.
Manufacturer and vendor  columns are just storing ids of records from tables Manufacturers and Vendors.

What I want to do:
In my "index" view - I'm displaying last 10 parts that were added. Right now in order to make it more human-friendly I'm substituting Vendor id which I'm storing in my Parts table for Vendor name which I'm pulling from Vendors table. Then in my View I'm comparing one the fly  my part.vendor to vendor.id. But that means I have to loop through Vendors array 10 times. Again considering the amount of data I'm working with - not a big hit, but what other ways are there to either implement it or to iterate through arrays.

The only other option I can think of is to do a join when I'm pulling data in a controller - then MySQL will do all the heavy lifting. But I'm not sure what will take a greater toll on my system.

Thoughts?
Below are snippets of my code.

Controller:

class PartsController < ApplicationController
  def index
    @parts = Part.find(:all, :limit =>10)
    @vendors = Vendor.find(:all)
    part_status_type = StatusType.find_by_name("part status").id
    @part_statuses = Status.find_all_by_status_type(part_status_type)
 
  end

View
<h3>Last ten parts added:</h3>
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#666666">
    <tr bgcolor="#cccccc">
        <th>Name</th>
        <th>Part #</th>
        <th>Vendor</th>
        <th>Vendor #</th>
        <th>Status</th>
        <th>Cost</th>
        <th>QTY</th>       
    </tr>
<% @parts.each do |p| %>
    <tr bgcolor="#ffffff">
        <td><%= p.name %></td>
        <td><%= p.id %></td>
        <td>
            <% @vendors.each do |v| %>
            <%     if v.id == p.vendor %>
                <%= v.name %>
            <% end %>   
            <% end %>   
        </td>
        <td><%= p.vendor_specific_id %></td>
        <td>
            <% @part_statuses.each do |s| %>
            <%     if s.id == p.part_status %>
                <%= s.name %>
            <% end %>   
            <% end %>   
        </td>   
        <td align="right">$<%= p.cost %></td>
        <td align="right"><%= p.total_quantity.to_i %></td>       
<% end %>

Re: Better way to itterate through array

You are not using ActiceRecord associations, are you ?

You should be using associations. They do all the joining and stuff for you. You should read up on associations, can't explain the whole thing in a post ...

Get a book.

Re: Better way to itterate through array

I found the pragmatic books helpful.

www.pragprog.com