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 id itemname itemdesc

table: BILLOFMATERIALS id item (which is an item in ITEMS table) component (which is also an item in ITEMS table) qty uom

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:

billofmaterials.item.itemname billofmaterials.component.itemname

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

BILLOFMATERIALS id item_id (to reference the ITEMS table) component

...thus allowing me to call: billofmaterials.item.itemname

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

end

class BillOfMaterial < ActiveRecord::Base

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

end

Yout bill_of_materials table should look like this:

id item_id item_type component_id component_type qty uom

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 = BillOfMaterial.new #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 b.save!

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.

Thorsten,

(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 boms):

bom.invitem.name

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

Merely calling for: bom.invitem_id

...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 end

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