Im designing an application which manages the renting of lots of different equipment. And I am wondering whats the best way to design the models for the application. My software has to manage lots of different types of equipment (with data types) for example:
Speaker
Make - String
Model - String
Wattage - Integer
Price - Decimal
Light
Make - String
Model - String
Wattage - Integer
Price - Decimal
Microphone
Make - String
Model - String
Use - Choice of: Instrumental, Vocal, Versatile
Price - Decimal
Cable
Length - Decimal
Connector 1 - String
Connector 2 - String
Price - Decimal
Stand
Type - Choice of: Microphone, Speaker
Height - Decimal
Boom - Boolean
Price - Decimal
Ways I have thought about the design:
But what is the best way in rails to handle these different types of products?
The Dynamic Attributes gem should allow you to do this automatically:
https://github.com/moiristo/dynamic_attributes
There may be better gems that do what you need, but this is the first I found.
If you're using Postgres as your database, then you can use hstore. There are gems to work with hstore. If you can afford, get a subscription to railscast and watch the screencast about implementing hstore.
Activerecord-postgres-hstore seems to be the go to gem for this.
I'd personally go with a single model Product and another model called ProductAttribute.
In this table, you'd have a name
column and a value
column.
This way, you're not limited by your schema. A product has n product_attributes
, named dynamically. You can in the admin section develop shortcuts so if you create a microphone product, it'll automatically create the specific attributes names in the linked table. You'd just have to input the values.
This way, your application is fully able to sell any sort of produts with any amount of attributes. No need to code again when in 3 months the manager will want to add another type of product :)
Edit : And of course, you'd have a ProductType
model to manage all the different product types you can sell.
Another option would be to make a product attributes table, and build each product type over an admin interface instead of in low-level code. That way you would not need to alter te application to sell new products.
This is a problem that has caused headaches to many vendors of ERP solutions before. The most elegant solution I would suggest to you based on what I've seen at one such vendor is this.
You define 4 models: Equipment, EquipmentType, Characteristic, Choice.
There would be a many-to-many relationship between Equipment and Characteristic, going through EquipmentType. The Characteristic model has an attribute called "value_type" and also one attribute for each value type you have (String, Integer, Decimal, Boolean). Finally, there would be a one-to-many relationship between Characteristic and Choice.
This is actually a watered-down version of that vendor's implementation which is suited to your particular requirements. That vendor's actual implementation is actually built at one or two levels of abstraction above what I'm showing you, in order to make the solution more generic. But those people are well-known for over-engineering things.
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With