Dynamic attributes!

I want to create a general model 'Product' that I will be able to store several attributes in it.

For example the attributes

:id :name :description

are pretty stantard. So each product will have this attributes.

However, I wanted to know if it is possible to create dynamic attributes. In the case of a cell phone device I might want to store the IMEI of the phone, this attribute is useless if the product is a laptop.

Is there any whey to create dynamic attributes ?

You may look into Single table inheritance to see if that'll suit your needs. Otherwise you could look into something like mongomapper and mongodb.

Matt W. wrote:

You may look into Single table inheritance to see if that'll suit your needs.

STI won't do the trick here. Multiple-table inheritance might.

Or you could have a separate Attribute model, or (ugly) put all the "extra" attributes into a serialized Hash.

But really, you should probably have several different tables, not the undifferentiated Product.

Otherwise you could look into something like mongomapper and mongodb.

Or CouchDB.

Best,

If you wanna search across this dynamic attributes, you might take a look at acts_as_solr_reloaded, it has support for dynamic attributes:

http://github.com/dcrec1/acts_as_solr_reloaded

you just need to run a migration:

script/generate dynamic_attributes_migration

and then define your model like this:

acts_as_solr :dynamic_attributes => true

This will anotate your model like:

has_many :dynamic_attributes

where DynamicAttribute is a class with name and value properties, like:

DynamicAttribute.new :name => 'imei', :value => '123456-12-123456-1'

Then you can search like this:

Product.search "imei:'123456-12-123456-1'"

More info here:

http://www.diegocarrion.com/2010/01/18/thinkingsphinx-exits-enters-actsassolrreloaded/

cheers