help with model relationship for Product/Bill of Materials

I am struggling to find the best way to set up relationships to achieve this so that it fits the rails idioms.

I have a table of products and I want to be able to create a bill of materials for a product..

So a product can either be a single item, or a package consisting of several other products.

I have created a table:

Tony, I'm currently working on the same thing but have decided to make BOM's a separate entity and not an item in the item file. Thus I have an Assembly table that has many Assyitems. These Assytitems hold foreign keys to the Item table. This works well when users don't have to do nested BOM's. Kathleen

Kathleen,

thanks for the suggestion. I thought about pulling BOM out to a separate model that would be used to reference the BOM Items. I went backwards and forwards a few times and decided to try it with just the bom_items and see if I was losing any functionality. Still putting together the view to allow BOMs to be created for a product, I will see how it works out.

Thanks Tony

Keeping your BomItem model pretty much as is you could have

class Product ...   has_many :bom_items   has_many :components, :through => :bom_items end

This gives you what you've already got.

Then you can say

has_many :bom_uses, :foreign_key => 'component_id', :class_name => 'BomItem' has_many :uses, :through => :bom_uses, :source => :product

(please think of better names than I have :slight_smile: Then given a production, p.components are the products it is made of, and p.uses is the products that uses it as a component

Fred