# Better way to itterate through array

**URL:** <https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475>\
**Category:** Rails\
**Created:** [August 21, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475 "2008-08-21T15:37:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/1 "2008-08-21T15:37:14Z")

</div>

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:

```auto
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

```auto

<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 %>

```

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/2 "2008-08-21T15:46:34Z")

</div>

On 21 Aug 2008, at 14:37, Kon U. wrote:

> Manufacturer and vendor columns are just storing ids of records from  
> 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.

Ugh. You could make @hash a vendor indexed by id and then you wouldn’t  
have to do the iterating over it. You’d still be loading all the  
vendors for nothing/

But why not make part have associations for vendor and manufacturer ?

Then you can do

@parts = Part.find(:all, :limit =\>10)

and then in your view  
\<%= p.vendor.name %\>

This will make one query each time to load the vendor, but you can  
defeat that by getting rails to load the vendors up front (and only  
those vendors actually used with

Part.find :all, :limit =\> 10, :include =\> :vendor

Fred

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/3 "2008-08-21T16:14:48Z")

</div>

Frederick C. wrote:

> On 21 Aug 2008, at 14:37, Kon U. wrote:
> 
> > Manufacturer and vendor columns are just storing ids of records from  
> > 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.
> 
> Ugh. You could make @hash a vendor indexed by id and then you wouldn’t  
> have to do the iterating over it. You’d still be loading all the  
> vendors for nothing/
> 
> But why not make part have associations for vendor and manufacturer ?
> 
> Then you can do
> 
> @parts = Part.find(:all, :limit =\>10)
> 
> and then in your view  
> \<%= p.vendor.name %\>
> 
> This will make one query each time to load the vendor, but you can  
> defeat that by getting rails to load the vendors up front (and only  
> those vendors actually used with
> 
> Part.find :all, :limit =\> 10, :include =\> :vendor
> 
> Fred

As I said - I’m just learning. Now i understand the dry aspect of  
associations. This is great!

Thank you Fred!

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/4 "2008-08-21T15:49:30Z")

</div>

First, prove it’s a performance issue. If it’s not, then don’t worry  
about  
it - it might smell funny but it may not be the most important thing in  
the  
world on your plate right now. Premature optimization, yadda yadda

Second, I’m reasonably sure you could do this all in sql code either  
with a  
find\_by\_sql, though I can’t really tell how your tables relate.

Third, Fred’s suggestion is really good.

Finally, investigate page, action, or fragment caching for this. This  
only  
needs to be queried when the data changes and chances are good that it  
changes less than it’s read. Even if this code never gets refactored,  
at  
least then you reduce the need to run the logic each time.

HTH!

On Thu, Aug 21, 2008 at 8:37 AM, Kon U.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/5 "2008-08-21T16:51:48Z")

</div>

On 21 Aug 2008, at 15:45, Kon U. wrote:

> Fred, now that I discovered associations I have a new question. Is  
> there  
> any way to create column name aliases.

the associations take a :foreign\_key option that says which column to  
use instead the default. Some examples at

> **[Creating multiple associations with the same table - Space Vatican](https://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table/)**
>
> Rails makes setting up your associations dead easy and very concise. There’s a whole bunch of options that can be set however as long as you …

Before you go any further you should really read up on associations -  
you’ll be creating a massive amount of extra work for yourself if you  
don’t

Fred

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/6 "2008-08-21T17:04:33Z")

</div>

Frederick C. wrote:

> On 21 Aug 2008, at 15:45, Kon U. wrote:
> 
> > Fred, now that I discovered associations I have a new question. Is  
> > there  
> > any way to create column name aliases.
> 
> the associations take a :foreign\_key option that says which column to  
> use instead the default. Some examples at  
> [Creating multiple associations with the same table - Space Vatican](http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table)
> 
> Before you go any further you should really read up on associations -  
> you’ll be creating a massive amount of extra work for yourself if you  
> don’t
> 
> Fred

Thanks I will.  
The book that I picked up didn’t go into too much detail - trying to  
find more info online.

Thanks for all your help.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [August 21, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/better-way-to-itterate-through-array/146475/7 "2008-08-21T16:45:17Z")

</div>

Frederick C. wrote:

> On 21 Aug 2008, at 14:37, Kon U. wrote:
> 
> > Manufacturer and vendor columns are just storing ids of records from  
> > 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.
> 
> Ugh. You could make @hash a vendor indexed by id and then you wouldn’t  
> have to do the iterating over it. You’d still be loading all the  
> vendors for nothing/
> 
> But why not make part have associations for vendor and manufacturer ?
> 
> Then you can do
> 
> @parts = Part.find(:all, :limit =\>10)
> 
> and then in your view  
> \<%= p.vendor.name %\>
> 
> This will make one query each time to load the vendor, but you can  
> defeat that by getting rails to load the vendors up front (and only  
> those vendors actually used with
> 
> Part.find :all, :limit =\> 10, :include =\> :vendor
> 
> Fred

Fred, now that I discovered associations I have a new question. Is there  
any way to create column name aliases.  
Here is what I mean. Below is a snippet from my interchangable\_part  
migration

class CreateInterchangableParts \< ActiveRecord::Migration  
def self.up  
create\_table :interchangable\_parts do |t|  
t.integer :interchanged\_part #this is the part the you want  
t.integer :interchangeable\_with\_part # can substitute / interchange  
with this part

t.timestamps  
end  
end

def self.down  
drop\_table :interchangable\_parts  
end  
end

Both of these columns need to refer to part\_id in part model. How would  
I setup association in Part model since I can’t call both of these  
columns part\_id?

Thanks
