Problem linking tables and accessing relationship heirarchy

I’m reposting this her as somebody suggested I posted in the wrong forum
before.

I’m not quite understanding this relationship thing between models and
tables. What I am trying to do within my view is get the price
information
by referring to it like this:

<%= @item.itemprice.Qty1 %>

In each of my individual views for the item_master table and the
itemprice
table I can get the info directly, but I can’t figure out how to access
the
price that corresponds to the item.

Here is my item.rhtml view test page:


Note: this works

<%= @item.itemDescription %>

This does not

<%= @item.itemprice.Qty1 %>

Below is all my Information:

My Models:


item_master.rb

class ItemMaster < ActiveRecord::Base
has_many :itemprices
has_many :item_sub_prices
has_many :item_price_specials
end


itemprice.rb

class itemprice < ActiveRecord::Base
belongs_to :item_master
end

My Controllers:


item_master_controller.rb

class ItemMasterController < ApplicationController

def show_all
@items = ItemMaster.find(:all)
end

def item
@item = ItemMaster.find(params[:id])
end
end


itemprice_controller.rb

class ItemPriceController < ApplicationController
def show
@price = itemprice.find(params[:id])
end
end


Thanks,

Brian

On 5 Aug 2008, at 17:00, Brian A. wrote:

<%= @item.itemprice.Qty1 %>

item_master.rb

class ItemMaster < ActiveRecord::Base
has_many :itemprices
has_many :item_sub_prices
has_many :item_price_specials
end

The has_manys you’ve added mean that @item.itemprices will be an array
of Itemprice objects, @item.itemprice (as you have found out) does not
exist.

Fred

Frederick C. wrote:

On 5 Aug 2008, at 17:00, Brian A. wrote:

[…]

The has_manys you’ve added mean that @item.itemprices will be an array
of Itemprice objects, @item.itemprice (as you have found out) does not
exist.

And what if there is a 1 to 1 relationship between tables? Then what
statement would be used in the item_master.rb instead of has_many?

On 5 Aug 2008, at 17:21, Brian A. wrote:

not
exist.

And what if there is a 1 to 1 relationship between tables? Then what
statement would be used in the item_master.rb instead of has_many?

belongs_to or has_one (the difference is where the foreign key is
located - the documentation on associations has got a bit about this)

Fred

Frederick C. wrote:

On 5 Aug 2008, at 17:21, Brian A. wrote:

belongs_to or has_one (the difference is where the foreign key is
located - the documentation on associations has got a bit about this)

Fred

Thank you Fred,

That helped quite a bit. One problem is that I had a table called
itemmaster with two fields one called ItemSerial and another called
ItemMasterID. This caused confusion with Ruby/Rails because my foreign
key was ItemSerial column.

I had to rename the ItemSerial field to id. Then when referencing the
price table Ruby/Rails wanted to reference a column called
item_master_id, so I had to rename my ItemSerial field to item_master_id
in the price table.

This was all confusing with the naming since I have a different column
called ItemMasterID that has nothing to do with the serial number.

It is confusing having to rename table columns, and not to be able to
directly reference them instead of using the default naming assumption.

-Brian