Brian
2007-Mar-26 13:20 UTC
Iterating over instance variable in ''index'' view producing unexpected results
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)
<table>
<tr>
<th>Category</th>
<th>Vendor</th>
...[snip]...
</tr>
<% for contract in @contracts %>
<tr>
<td><%= contract.lu_contract_category.name %></td>
<td><%= contract.lu_vendor.name %></td>
...[snip]...
</tr>
<% end %>
</table>
-------------------------------------------------------------------
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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Brian
2007-Mar-27 22:28 UTC
Re: Iterating over instance variable in ''index'' view producing unexpected results
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 On 3/26/07, Brian <backslash.00-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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) > > <table> > <tr> > <th>Category</th> > <th>Vendor</th> > ...[snip]... > </tr> > <% for contract in @contracts %> > <tr> > <td><%= contract.lu_contract_category.name %></td> > <td><%= contract.lu_vendor.name %></td> > ...[snip]... > </tr> > <% end %> > </table> > > ------------------------------------------------------------------- > > 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. >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nullset
2007-Mar-28 11:25 UTC
Re: Iterating over instance variable in ''index'' view producing unexpected results
Issue resolved by swapping belongs_to & has_one in the models... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---