views:

800

answers:

2

If you come across classifieds websites like craigslist, oodle, kijiji,... everything is a classified, when you are posting a classified, they show different form fields depending upon the category one selects. For example, if you are posting a classified about Car, they show make, model, interiors, etc... How do you think data is handled in the database?

Do you think they are creating separate table for each type of classified/ or everything is one table "Classifieds"? If the data is handled using one table (STI) there must be columns for each and every scenario (type of classified) and all the fields might not be used for every classified.

What is the best way to design the ActiveRecord objects to handle to have similar classifieds website?

A: 

They likely have separate tables. While they are all classifieds and they are "categories" to the user, as you say the data is very different and I would suspect that they are stored separately because of that. There would be no real advantage (that I can think of) to storing them all in one big table when most of the time people will be querying an individual table. The disadvantages, as you say, would be all the columns for all types would need to be in that one table which would create a wide table with a lot of null data.

The categories themselves are pretty fixed so adding new ones is likely pretty rare. The other way you do it is to have defined properties associated to the one big table and templates of dynamic properties (Classified has many Properties). But that would probably not be good from a performance point of view.

geofflane
Is it good to store the attribute types (for example: make types of car, model types of car) in the table (with acts_as_list and acts_as_tree) or store them as class constants? Remember some of these attribute types may need to added at runtime.
satynos
The attribute types are probably just the name (or a combo of user visible and computer name) as a key. The values just get stored as strings then. It basically will give you a set of key-value pairs. When someone searches for 'Ford' you don't really care where it comes from right? Just that the value is found.Does it really need to be dynamic though? Can't you set the superset of properties on a top-level category basis? Housing has everything for the sub-categories rental, etc.
geofflane
+1  A: 

There is undoubtedly a source specifying the basic schema for each type of Classified. If you're implementing such a system yourself, you may be aided by persisting the individual records with a schema-free persistence layer such a CouchDB.

Doing so means adding or removing attributes to a given schema type need not affect older records.

Duncan Beevers
i like your answer Duncan, however this is hella hard to do, isn't it? especially in Rails? PM me if you have a solution, as i'm at a loss at this exact situation right now.
pjammer