# Help with model relationship for Product/Bill of Materials

**URL:** <https://www.ruby-forum.com/t/help-with-model-relationship-for-product-bill-of-materials/134290>\
**Category:** Rails\
**Created:** [April 9, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/help-with-model-relationship-for-product-bill-of-materials/134290 "2008-04-09T15:50:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [April 9, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/help-with-model-relationship-for-product-bill-of-materials/134290/1 "2008-04-09T15:50:35Z")

</div>

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:

* * *

class BomItem \< ActiveRecord::Base

belongs\_to :product

belongs\_to :component,  
:class\_name=\>‘Product’,  
:foreign\_key=\>‘component\_id’

## validates\_numericality\_of :quantity

So for a product p, I can have:  
bom=p.bom\_items  
item=bom[0] etc

and for each item I can have:  
c=item.component (which is a product)

Now for c I would like to be able to ask  
c.component\_of

at the moment, I can only think to set up a product method

Perhaps that is the best way to do it, but I just feel I am not  
modeling this in the best way. I also want to make sure a product  
cannot have a bom and be a component in the same bom - since that is  
circular

If any experts in data modeling have any suggestions or advice I’d be  
grateful

ps. I did have one bright idea to sub class product into component  
and packet, which worked really well from console tests, but messed up  
my Product views because although @product=Product.find id, the url  
for product\_path(@product) becomes packet path if it has type=‘Packet’  
and of course there is no such route. That did my head in so I  
dropped the idea.

Tonypm

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [April 9, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/help-with-model-relationship-for-product-bill-of-materials/134290/2 "2008-04-09T16:14:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [April 10, 2008, 10:05am UTC](https://www.ruby-forum.com/t/help-with-model-relationship-for-product-bill-of-materials/134290/3 "2008-04-10T10:05:11Z")

</div>

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 🙂  
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

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [April 10, 2008, 9:46am UTC](https://www.ruby-forum.com/t/help-with-model-relationship-for-product-bill-of-materials/134290/4 "2008-04-10T09:46:43Z")

</div>

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
