# Dynamic attributes!

**URL:** <https://www.ruby-forum.com/t/dynamic-attributes/182209>\
**Category:** Rails\
**Created:** [January 25, 2010, 8:54am UTC](https://www.ruby-forum.com/t/dynamic-attributes/182209 "2010-01-25T08:54:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![demec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@demec](https://www.ruby-forum.com/u/demec)\
**Post date:** [January 25, 2010, 8:54am UTC](https://www.ruby-forum.com/t/dynamic-attributes/182209/1 "2010-01-25T08:54:21Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![demec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@demec](https://www.ruby-forum.com/u/demec)\
**Post date:** [January 25, 2010, 9:20am UTC](https://www.ruby-forum.com/t/dynamic-attributes/182209/2 "2010-01-25T09:20:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![demec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@demec](https://www.ruby-forum.com/u/demec)\
**Post date:** [January 25, 2010, 7:23pm UTC](https://www.ruby-forum.com/t/dynamic-attributes/182209/3 "2010-01-25T19:23:49Z")

</div>

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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![demec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@demec](https://www.ruby-forum.com/u/demec)\
**Post date:** [January 25, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/dynamic-attributes/182209/4 "2010-01-25T21:07:55Z")

</div>

If you wanna search across this dynamic attributes, you might take a  
look at acts\_as\_solr\_reloaded, it has support for dynamic attributes:

> **[GitHub - dcrec1/acts\_as\_solr\_reloaded: ActsAsSolr with new features](https://github.com/dcrec1/acts_as_solr_reloaded)**
>
> ActsAsSolr with new features. Contribute to dcrec1/acts\_as\_solr\_reloaded development by creating an account on GitHub.

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/](http://www.diegocarrion.com/2010/01/18/thinkingsphinx-exits-enters-actsassolrreloaded/)

cheers
