Tables, Fields, and Naming Conventions

Is there a way to have a table with two references to the another table
using two fields?

Here is what I mean. I have two tables, items and billofmaterials:

table: ITEMS

item (which is an item in ITEMS table)
component (which is also an item in ITEMS table)

For a single record of BILLOFMATERIALS, both of the fields “item” and
“component” are items from the ITEMS table. In my html and such I
would like to call the equivalent of:


But I can not due to ROR naming conventions. Rather, I need to create
my BILLOFMATERIALS table like:

item_id (to reference the ITEMS table)

…thus allowing me to call:

But how can I do the same for the “component” field in BILLOFMATERIALS,
as I certainly can not have two fields called “item_id”.

The Answer should be Polymorphic assosiations.

class Item < ActiveRecord::Base

has_many :bill_of_materials, :as => :component
has_many :bill_of_materials, :as => :item


class BillOfMaterial < ActiveRecord::Base

belongs_to :item, :polymorphic => true
belongs_to :component, :polymorphic => true


Yout bill_of_materials table should look like this:


The *_type fields are required for the polymorphic association and
handled my ActiveRecord, you dont have to give any values…

you could now do this:

i = Item.find(1) #get an Item
c = Item.find(2) #get another Item, this will be the “component”
b = #create a new BillOfMaterial object
b.item = i # set this Item as Item of the new Bill… object
b.component = c #set this Item as Component in the new Bill… object!

i hope i didnt twist up any singular/plural stuff, or any other errors.
never tested this polymorphic stuff really, bu i think this should be
the way to go.


(please note that what follows are the actual names of my objects and
db tables, compared to my original post…sorry for the
confusion…the layout is still the exact same)

Thanks so much for your detailed response. I have been knocking away
at this since your post, with near-success. I am able to save new bom
(billofmaterial) records, and the invitem_id and component_id fields
are populating in the db with the appropriate id’s from the INVITEMS
table. But I am not able to use (for example, in my basic view for

I get:
You have a nil object when you didn’t expect it!
The error occured while evaluating

Merely calling for:

…works fine though. Now, in looking around for a cause, I see that
invitem_type and component_type in the boms table are populating with
NULL. I know you mentioned… “The *_type fields are required for the
polymorphic association and handled my ActiveRecord, you dont have to
give any values…”. But, ROR is probably supposed to put something
in those field other than NULL, right? Something to do with the object
that is expected in invitem_id and component_id?

So I am trying to see where my error in the config is. I have:

class Bom < ActiveRecord::Base
belongs_to :invitem, :polymorphic => true
belongs_to :component, :polymorphic => true

class Invitem < ActiveRecord::Base
belongs_to :itemtype
belongs_to :vendor
has_many :boms, :as => :invitem
has_many :boms, :as => :component

mysql> desc boms;
| Field | Type
| id | int(11)
| date_added | date
| invitem_id | varchar(20)
| invitem_type | varchar(20)
| component_id | varchar(20)
| component_type | varchar(20)
| qty | decimal(10,2)
| uom | varchar(5)

One question that pops to mind is, in my Bom.rb class, how does ROR
know what “belongs_to :component” does? Is that some under-the-cover
magic? Should I be expected to have a Component.rb somewhere? I don’t
necessarily want one…as “component” is only a field in the boms
table…and in reality “is an invitem”.

Could the fact that I am using “has_many :boms, :as => :invitem” be
causing conflict, as I actually have a class called Invitem? I know
the part “…:as => :invitem” is supposed to be an arbitrary name.

Hmmm? What might I be missing here?

Thanks so much for the response.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs