# Iterating over instance variable in 'index' view producing u

**URL:** <https://www.ruby-forum.com/t/iterating-over-instance-variable-in-index-view-producing-u/90161>\
**Category:** Rails\
**Created:** [March 26, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/iterating-over-instance-variable-in-index-view-producing-u/90161 "2007-03-26T15:22:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 26, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/iterating-over-instance-variable-in-index-view-producing-u/90161/1 "2007-03-26T15:22:01Z")

</div>

Hello All,

I am running into some problems with my ‘index’ view of an application I  
am  
trying desperately to convert from PHP to Ruby/Rails.

\*\* MY ENVIRONMENT \*\*

* * *

Table: contracts

mysql\> describe contracts;

±------------------±-----------------±-----±----±-----------±---------------+  
| Field | Type | Null | Key | Default |  
Extra |

±------------------±-----------------±-----±----±-----------±---------------+  
| id | int(11) unsigned | NO | PRI | NULL |  
auto\_increment |  
| category\_id | int(11) unsigned | NO | MUL |  
| |  
| vendor\_id | int(11) unsigned | NO | MUL |  
| |  
| created\_at | datetime | NO | |  
| |  
| updated\_at | datetime | NO | |  
| |

±------------------±-----------------±-----±----±-----------±---------------+

Controller: Contracts (index method)

class ContractsController \< ApplicationController  
def index  
@contracts = Contract.find(:all)  
…  
end

Model: Contract

# :foreign\_key added because the database isn’t 100%

‘rails-convention-compliant’  
class Contract \< ActiveRecord::Base  
has\_one :lu\_contract\_category,  
:foreign\_key =\> “id”  
has\_one :lu\_vendor,  
:foreign\_key =\> “id”  
end

* * *

Table: lu\_contract\_categories

mysql\> describe lu\_contract\_categories;  
±------±-----------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------±-----------------±-----±----±--------±---------------+  
| id | int(11) unsigned | NO | PRI | NULL | auto\_increment |  
| name | varchar(50) | NO | | | |  
±------±-----------------±-----±----±--------±---------------+

Model:LuContractCategory

# :foreign\_key added because the database isn’t 100%

‘rails-convention-compliant’  
class LuContractCategory \< ActiveRecord::Base  
belongs\_to :contract,  
:foreign\_key =\> “category\_id”  
end

* * *

Table: lu\_vendors

mysql\> describe lu\_vendors;  
±------±-----------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------±-----------------±-----±----±--------±---------------+  
| id | int(11) unsigned | NO | PRI | NULL | auto\_increment |  
| name | varchar(50) | NO | | | |  
±------±-----------------±-----±----±--------±---------------+

Model: LuVendor

# :foreign\_key added because the database isn’t 100%

‘rails-convention-compliant’  
class LuVendor \< ActiveRecord::Base  
belongs\_to :contract,  
:foreign\_key =\> “vendor\_id”  
end

* * *

View: Index (index.rhtml for Contracts)

 ...[snip]... \<% for contract in @contracts %\> ...[snip]... \<% end %\> 

| Category | Vendor |
| --- | --- |
| \<%= contract.lu\_contract\_category.name %\> | \<%= contract.lu\_vendor.name %\> |

* * *

The follwoing query:

mysql\> SELECT lu\_contract\_categories.name AS Category, lu\_vendors.name  
AS  
Vendor  
-\> FROM contracts  
-\> INNER JOIN lu\_contract\_categories ON (contracts.category\_id =  
lu\_contract\_categories.id)  
-\> INNER JOIN lu\_vendors ON (contracts.vendor\_id = lu\_vendors.id);

Produces the expected:

±---------±-----------------+  
| Category | Vendor |  
±---------±-----------------+  
| Hardware | Sun Microsystems |  
| Hardware | Sun Microsystems |  
±---------±-----------------+

\*\* THE PROBLEM \*\*

However, in my ‘index’ view, I get:

±---------±-----------------+  
| Category | Vendor |  
±---------±-----------------+  
| Hardware | Sun Microsystems |  
| Software | IBM |  
±---------±-----------------+

It seems as contracts instance variable (@contracts) is iterated over in  
the  
view, the category & vendor records are being selected based upon the id  
(PK) of the contract record.

This is really kicking my butt… Can anyone point out where the major  
problem is - I’m guessing the view?

Any help is appreciated.

TIA!

- Brian

Btw… My ‘new’ and ‘edit’ actions work perfectly.

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 28, 2007, 12:48am UTC](https://www.ruby-forum.com/t/iterating-over-instance-variable-in-index-view-producing-u/90161/2 "2007-03-28T00:48:26Z")

</div>

Anybody?

FYI, I started over again from the beginning. I used all rails naming  
conventions.

Still experiencing the same issue…

Any help is appreciated.

- Brian

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 28, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/iterating-over-instance-variable-in-index-view-producing-u/90161/3 "2007-03-28T13:26:59Z")

</div>

Issue resolved by swapping belongs\_to & has\_one in the models…
