# Storing a hash in the data base

**URL:** <https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019>\
**Category:** Rails\
**Created:** [December 13, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019 "2007-12-13T13:38:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019/1 "2007-12-13T13:38:14Z")

</div>

Hi  
I’m new to rails and I want to create an object named element with can  
hold sevral properties names and values.  
for example :

e = Element.create()  
e.add(:name =\> “x”, :value =\> 800)  
e.add(:name =\> “y”, :value =\> 600)  
e.add(:name =\> “whatever”, :value =\> “anything”)

e.get(:name =\> “x”) #-\> 800  
e.get(:name =\> “y”) #-\> 300  
e.get(:name =\> “whatever”) #-\> anything

i was thinkings add the object var which holds (id, element\_id, name,  
value)  
with a relationship of element has\_many vars.  
how do i create the getter ? (e.get(:name =\> “x”)  
and the setter ?

thanks.

anyone used this plugin [http://svn.nkryptic.com/](http://svn.nkryptic.com/) ?

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019/2 "2007-12-13T14:10:56Z")

</div>

Gady,

This is a perfect area where Ruby can work its magic. Here’s an  
example of what you could do:

class Element \< ActiveRecord::Base  
has\_many :variables

def   
variables.find\_by\_name(name)  
end

def []=(name,value)  
var = variables.find\_or\_create\_by\_name(name)  
var.value = value  
var.save  
end  
end

class Variable \< ActiveRecord::Base  
belongs\_to :element  
end

Now you will be able to do the following:

e = Element.create  
e[“x”] = 800  
e[“y”] = 600  
e[“whatever”] = “anything”

e[“x”] #-\> “800”  
e[“y”] #-\> “600”  
e[“whatever”] #-\> “anything”

Isn’t that fantastic? Note that this is assuming that your value field  
is a TEXT which will return anything as a string. You could also store  
a type and coerce the values, let me know if you want more detail.

Michael B.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 13, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019/3 "2007-12-13T14:55:52Z")

</div>

Thanks allot.  
never thought it can be that easy…  
there is a small bug. it need to be

def   
variables.find\_by\_name(name).value  
end

Michael B. wrote:

> Gady,
> 
> This is a perfect area where Ruby can work its magic. Here’s an  
> example of what you could do:
> 
> class Element \< ActiveRecord::Base  
> has\_many :variables
> 
> def   
> variables.find\_by\_name(name)  
> end

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 14, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019/4 "2007-12-14T13:41:04Z")

</div>

Sorry, forgot to mention that ‘variables’ must be a text attribute on  
Element.

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [December 14, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-the-data-base/123019/5 "2007-12-14T13:38:04Z")

</div>

Hi Gady,

Another (much lighter) way of doing this is with the  
'serliaze’declaration, that serializes hashes or arrays automagically.

class Element \< ActiveRecord::Base  
serialize :variables  
end

Then you can just do setters with:

element.variables = {:price =\> ‘4’, :availability =\> ‘7 Days’}  
element.save

And the getter becomes:  
element.variables[:price]

This way, you don’t need additional classes, tables or relationships.

Matt

On Dec 13, 10:38 pm, Gady S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
