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 %>
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
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!
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.