Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails way for multi product types shopping cart

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:

  • An individual model for each type of product then a polymorphic association in the cart so that it can handle all the types of equipment.
  • A single product model that has fields for all types of equipment with a type field which can be checked when ever the product is used.
  • A product model with a price attribute then every type of product extends that model.

But what is the best way in rails to handle these different types of products?

like image 421
Dean Avatar asked Jun 16 '12 17:06

Dean


4 Answers

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.

like image 85
seanyboy Avatar answered Nov 11 '22 05:11

seanyboy


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.

like image 24
Anthony Alberto Avatar answered Nov 11 '22 06:11

Anthony Alberto


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.

like image 43
DVG Avatar answered Nov 11 '22 06:11

DVG


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.

like image 32
Toshio Avatar answered Nov 11 '22 05:11

Toshio