ruby - Best way to implement superclass subclass model based design in rails database? -
suppose designing database online-shop, sell different kind of items. every item has own distinct property, beside few in common price, manufacturer, etc. can segregated superclass (common fields) , subclass (distinct fields)
say have following tables:
product manufacturer:decimal pen ink:string shirt size:int making product_id field in both pen , shirt table solve problem, there other way that? , idea here segregate superclass , subclass?
the common approach in situation use single table inheritance(sti). product base class:
class product < activerecord::base end and pen/shirt subclasses:
class pen < product end class shirt < product end usually information being stored in base product table(that why it's single table inheritance) , differentiated of special type column(of course have nils in ink field shirts).
if want store common fields in products table, can create separate tables subclasses. then, think need not specify type, specific_product_id in order able pull complete information product(it in between sti , polymorphic association). this:
product id (product_fields) type specific_product_id 1 ... pen 2 2 ... shirt 7 pen id (pen_fields) 2 ... shirt id (shirt_fields) 7 ... decision you.
Comments
Post a Comment