# Marshalling data

**URL:** <https://www.ruby-forum.com/t/marshalling-data/90127>\
**Category:** Rails\
**Created:** [March 26, 2007, 8:17am UTC](https://www.ruby-forum.com/t/marshalling-data/90127 "2007-03-26T08:17:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [March 26, 2007, 8:17am UTC](https://www.ruby-forum.com/t/marshalling-data/90127/1 "2007-03-26T08:17:03Z")

</div>

Hello list,

For my website, I’m building a module-based customizable system, so  
users can pick their own modules and display them on their profile  
page. I want to make this as componentized as possible, but the  
following has me scratching my head.  
My module table has an options TEXT field, in which I’d like to store  
marshalled options data for that specific module. I’ve set up  
everything so I can make custom templates for each module, and use  
the data provided in the options field. What I can’t get to work is  
the following. An example. One of the modules is a Last.fm browse  
recently played tracks module. For that, I need to store the last.fm  
username of the user in the db. So, options =\> { :username =\>  
“foo” }. I’d like to be able to use it directly in my view, using  
something like \<%= text\_field ‘options’, ‘username’ %\>. I seem to  
lack some understanding of the way Marshal.load and .dump creates  
data structures, so I really can’t get this to work. I’ve tried  
things like:

@options = { :username =\> “test” }  
str = Marshal.dump(@options)  
@options = Marshal.load(str)

But that doesn’t work. Can anybody give me some hints?

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [March 26, 2007, 10:09am UTC](https://www.ruby-forum.com/t/marshalling-data/90127/2 "2007-03-26T10:09:37Z")

</div>

Bart Z. wrote:

> the following. An example. One of the modules is a Last.fm browse  
> @options = Marshal.load(str)
> 
> But that doesn’t work. Can anybody give me some hints?
> 
> Thanks a bunch!

Works for me:

[281]script/console  
Loading development environment.

> > @options = { :username =\> “test” }  
> > =\> {:username=\>“test”}
> 
> > str = Marshal.dump(@options)  
> > =\> “\004\b{\006:\rusername”\ttest"
> 
> > @options = Marshal.load(str)  
> > =\> {:username=\>“test”}
> 
> > @options[:username]  
> > =\> “test”

What happens when you try it in script/console or irb?

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [April 1, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/marshalling-data/90127/3 "2007-04-01T17:28:14Z")

</div>

Michael W. wrote:

> Bart Z. wrote:
> 
> > the following. An example. One of the modules is a Last.fm browse  
> > @options = Marshal.load(str)
> > 
> > But that doesn’t work. Can anybody give me some hints?
> > 
> > Thanks a bunch!
> 
> Works for me:
> 
> [281]script/console  
> Loading development environment.
> 
> > > @options = { :username =\> “test” }

Erf, must have been a brain fart… It works in irb or the console.

However, when I try this in my controller:

```
@extra_modules = ExtraModule.find(:all)

@options = []
@extra_modules.each do |m|
  @options[m.id] = Marshal.load(m.options)
end

```

I get the error: instance of IO needed…

Any idea?

---

<div class="post-metadata">

**Author:** ![bartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bartz](https://www.ruby-forum.com/u/bartz)\
**Post date:** [April 1, 2007, 5:34pm UTC](https://www.ruby-forum.com/t/marshalling-data/90127/4 "2007-04-01T17:34:40Z")

</div>

> Erf, must have been a brain fart… It works in irb or the console.
> 
> However, when I try this in my controller:
> 
> ```
> @extra_modules = ExtraModule.find(:all)
> 
> @options = []
> @extra_modules.each do |m|
> @options[m.id] = Marshal.load(m.options)
> end
> 
> ```

Ok, found it. Apparently instance.options doesn’t work, but  
instance.custom\_options for instance does.  
Glad it finally works!
