# Saving \*args to database

**URL:** <https://www.ruby-forum.com/t/saving-args-to-database/116327>\
**Category:** Rails\
**Created:** [October 4, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327 "2007-10-04T14:39:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [October 4, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327/1 "2007-10-04T14:39:47Z")

</div>

Hi,

I am trying to find a way to save arguments (\*args) of a method in  
database.  
So that when retrieving values from database, coupled with the method  
name, I would be able to go:  
foo.send(method, \*args)

Is it possible to use binary to save this args object?  
Would there not be instantiation problems for arguments, so I would not  
use the right object type when retrieving their values?

I could not find any similar process on the web. And I am not sure in  
which direction to go…

Any thought would be helpful !

Thanks

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [October 4, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327/2 "2007-10-04T14:50:20Z")

</div>

On 10/4/07, Fred P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Would there not be instantiation problems for arguments, so I would not  
> use the right object type when retrieving their values?

I’m not sure exactly what you’re asking. Perhaps you want  
serialization of objects in text columns? Look at the docs for  
ActiveRecord::Base, or Google “activerecord serialize”

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [October 4, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327/3 "2007-10-04T14:56:47Z")

</div>

On 4 Oct 2007, at 13:39, Fred P. wrote:

> Would there not be instantiation problems for arguments, so I would  
> not  
> use the right object type when retrieving their values?

You can use Marshal.dump/Marshal.load to serialize objects.  
ActiveRecord also has the serialize method:

class Foo \< ActiveRecord::Base  
serialize :bar  
end

upon saving bar will be serialized using yaml.  
Some objects (eg procs) can’t be dumped.

Fred

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [October 4, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327/4 "2007-10-04T15:05:38Z")

</div>

If we understand you correctly you should serialize args to a database  
field and desterilize it when needed.  
You could use json format for this serialization/destabilization  
purpose.  
Anyhow, what ever formats you choose stay a way from binary one.

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [October 4, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327/5 "2007-10-04T15:28:25Z")

</div>

dima wrote:

> If we understand you correctly you should serialize args to a database  
> field and desterilize it when needed.  
> You could use json format for this serialization/destabilization  
> purpose.  
> Anyhow, what ever formats you choose stay a way from binary one.

Ok I’ll try to use that serialization.

Thanks for your help! This is what I was looking for.

Fred

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [October 5, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/saving-args-to-database/116327/6 "2007-10-05T12:50:45Z")

</div>

I all works just fine!

Impressive the way “serialize :args” did all the work for me… whatever  
type it is in the array when saving, it will give me the same one back  
on deserialization.

Thanks for your great input.

Fred
