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)
Category | Vendor | ...[snip]...
---|---|
<%= contract.lu_contract_category.name %> | <%= contract.lu_vendor.name %> | ...[snip]...
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.