Marshalling data

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!

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.

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?

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!