# Weirdness when save symbols to AR string fields

**URL:** <https://www.ruby-forum.com/t/weirdness-when-save-symbols-to-ar-string-fields/82804>\
**Category:** Rails\
**Created:** [January 21, 2007, 6:30am UTC](https://www.ruby-forum.com/t/weirdness-when-save-symbols-to-ar-string-fields/82804 "2007-01-21T06:30:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 21, 2007, 6:30am UTC](https://www.ruby-forum.com/t/weirdness-when-save-symbols-to-ar-string-fields/82804/1 "2007-01-21T06:30:46Z")

</div>

I have a model foo with two fields: id and string. When I try to save a  
symbol into that string field I get weird results:

> > foo = Foo.new(:string =\> :foobar)  
> > foo.string  
> > =\> :foobar
> 
> > foo.save  
> > foo.string  
> > =\> :foobar
> 
> > foo = Foo.find(foo.id)  
> > =\> #\<Foo:0x467282c @attributes={“id”=\>5, “string”=\>"— :foobar\n"}\>
> 
> > foo.string  
> > =\> “— :foobar\n”

Apparently the database save is converting the symbol into YAML. But I  
don’t know why and I can’t seem to unmarshal it. Any idea what’s going  
on?

Thanks!

/adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 21, 2007, 8:35am UTC](https://www.ruby-forum.com/t/weirdness-when-save-symbols-to-ar-string-fields/82804/2 "2007-01-21T08:35:27Z")

</div>

On 1/20/07, Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > foo = Foo.find(foo.id)  
> > > =\> #\<Foo:0x467282c @attributes={“id”=\>5, “string”=\>“— :foobar\n”}\>  
> > > foo.string  
> > > =\> “— :foobar\n”
> 
> Apparently the database save is converting the symbol into YAML. But I  
> don’t know why and I can’t seem to unmarshal it. Any idea what’s going  
> on?

Yea, it serializes non-db fields to yaml. Use the serialize method to  
unserialize it for you.

[http://rails.rubyonrails.org/classes/ActiveRecord/Base.html#M001004](http://rails.rubyonrails.org/classes/ActiveRecord/Base.html#M001004)

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
