Rails: how do you handle dynamic attributes in a database?

If you come across ad sites such as craigslist, oodle, kijiji, ... everything is classified, when you publish classified, they show different form fields depending on the category selected. For example, if you publish a classified car, they show the make, model, interiors, etc. Do you think the data is processed in a database?

Do you think they create a separate table for each type of classified / or all have one table "Ads"? If the data is processed using one table (STI), there must be columns for each scenario (classification type), and all fields may not be used for each category.

What is the best way to create ActiveRecord objects to work with similar ad sites?

+4
source share
2 answers

They probably have separate tables. Although they are all ads, and they are “categories” for the user, as you say, the data is very different, and I suspect that they are stored separately because of this. There would be no real advantage (what I can think of) to store them all in one big table, when most of the time people will request a separate table. The disadvantages, as you say, would be all the columns for all the types that should have been in that single table, which would create a wide table with a lot of null data.

The categories themselves are quite fixed, so adding new ones is most likely quite rare. Another way you do this is to define specific properties associated with one large table and dynamic property templates (a class has many properties). But that probably won't be good in terms of performance.

0
source

There is no doubt a source that defines the basic layout for each type of ad. If you implement such a system yourself, it can help you by keeping separate records with a circuit-free free level, such as CouchDB .

This means that adding or removing attributes for a given type of schema should not affect old records.

+1
source

All Articles