# AR overzealous type conversion

**URL:** <https://www.ruby-forum.com/t/ar-overzealous-type-conversion/188238>\
**Category:** Rails\
**Created:** [May 8, 2010, 6:21am UTC](https://www.ruby-forum.com/t/ar-overzealous-type-conversion/188238 "2010-05-08T06:21:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [May 8, 2010, 6:21am UTC](https://www.ruby-forum.com/t/ar-overzealous-type-conversion/188238/1 "2010-05-08T06:21:24Z")

</div>

Here’s an ActiveRecord oddity. If I call WebPageCache.create(:key =\>  
[1, 2, 3]), AR appears to store a key of [1,2,3].to\_yaml even though I  
have not specified ‘serialize :key’. Is this documented? If not, it  
feels like a bug:  
======== schema:  
create\_table “web\_page\_caches”, :force =\> true do |t|  
t.string “key”  
t.binary “value”, :limit =\> 16777215  
t.datetime “expiration”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end  
======== model:  
class WebPageCache \< ActiveRecord::Base  
end  
======== results:

> > WebPageCache.create(:key=\>[1, 2, 3])  
> > =\> #\<WebPageCache id: 297, key: [1, 2, 3], value: nil, expiration: nil,  
> > created\_at: “2010-05-08 03:46:04”, updated\_at: “2010-05-08 03:46:04”\>
> 
> > WebPageCache.count  
> > =\> 1
> 
> > WebPageCache.first  
> > =\> #\<WebPageCache id: 297, key: “— \n- 1\n- 2\n- 3\n”, value: nil,  
> > expiration: nil, created\_at: “2010-05-08 03:46:04”, updated\_at:  
> > “2010-05-08 03:46:04”\>  
> > WebPageCache.serialized\_attributes  
> > =\> {}  
> > ========  
> > AR appears to be yaml-fying :key=\>[1,2,3] rather than giving a error.

The docs say:  
| Active Record can serialize any object in text columns using  
| YAML. To do so, you must specify this with a call to the class  
| method serialize.

To be pedantic, the docs don’t say what will happen if you don’t specify  
‘serialize :key’, but I’d expect an error or something. I’m writing a  
test suite, so it would be handy to test the published behavior.

Any ideas? Pointers?

thanks.

# Oh - I almost forgot:

bash-3.2$ rails --version ; ruby --version ; mysql --version  
Rails 2.3.5  
ruby 1.9.1p376 (2009-12-07 revision 26041) [powerpc-darwin9.8.0]  
mysql Ver 14.14 Distrib 5.1.36, for apple-darwin9.5.0 (powerpc) using  
readline 5.1
