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