# AR serialize and MySQL, truncation

**URL:** <https://www.ruby-forum.com/t/ar-serialize-and-mysql-truncation/144151>\
**Category:** Rails\
**Created:** [July 25, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/ar-serialize-and-mysql-truncation/144151 "2008-07-25T23:08:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 25, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/ar-serialize-and-mysql-truncation/144151/1 "2008-07-25T23:08:38Z")

</div>

I am still using latest Rails 1.8, not 2.

Using the “serialize :attribute\_name” method on an ActiveRecord, if the  
serialization ends up being too long for the field, with a MySQL  
database, it will just be silently truncated. Of course, this makes it  
so it can’t be unserialized again, and you end up with a raw string in  
the attribute instead of actually the unserialized object you expected.

Is there any easy way to get an exception to be raised on save if the  
serialized value will be truncated?

Sadly, validates\_length\_of doesn’t do it, because it tries to call  
#length on the pre-serialized object, which depending on the class of  
the object will give an irrelevant value (Hash.length or Array.length),  
or even raise a method not found exception.

Any ideas?

Jonathan
