Answer the question
In order to leave comments, you need to log in
How to organize a catalog of different types of goods?
Good afternoon.
Head example. There is a store directory. It contains products belonging to different categories, but some of the parameters are the same for all products. For example: stoves, TVs and multicookers. Fields - description, price, dimensions, weight are both in the first type and in the second and third. But each type has its own - unique parameters. The TV has a diagonal, the multicooker has the number of programs, and the stove has the number of burners. The question is how to organize the storage and processing of all this stuff. Each product has its own model? But then there will be a lot of code to repeat. One model for everything, with a field belonging to a certain type of product? It also seems irrational - for each product, most of the fields will be empty due to such universality, and it is easy to get confused in so many parameters.
Thoughts of inheritance come to mind, but I heard that it is highly discouraged. I recently learned about concerns (I'm a very beginner developer), but they seem to be for methods, not data. In general - how to be?
Answer the question
In order to leave comments, you need to log in
You can make a Products model and a Parameters model and link them many-to-many through the ProductsParameters model in which the parameter value will be stored
Modern databases, such as PostgreSQL, have special data types for such cases - JSON, hstore. Look in their direction. They are very convenient, but may be inferior to regular fields in terms of performance.
In any case, you will have to solve the issue of validations, otherwise the model will turn into an unreadable mess of conditional validations. Here it is either STI, or validations are taken out into separate objects - form objects.
Purely theoretically - what are common parameters and what are unique parameters?
Actually, the latter differ from the former only in that it is necessary to store and display not only the value, but also the name of the parameter.
The same unique parameters that require something more (formatted text, photo-video, maps, etc.) - it's better to put them in the general ones. There won't be that many.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question