# Problem linking tables and accessing relationship heirarchy

**URL:** <https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062>\
**Category:** Rails\
**Created:** [August 5, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062 "2008-08-05T18:00:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 5, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062/1 "2008-08-05T18:00:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 5, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062/2 "2008-08-05T18:15:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 5, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062/3 "2008-08-05T18:21:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 5, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062/4 "2008-08-05T18:25:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [August 5, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/problem-linking-tables-and-accessing-relationship-heirarchy/145062/5 "2008-08-05T18:52:27Z")

</div>

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
