# Time Zone / Active Record Persistence Error (Windows Only)

**URL:** <https://www.ruby-forum.com/t/time-zone-active-record-persistence-error-windows-only/153345>\
**Category:** Rails\
**Created:** [November 15, 2008, 6:47am UTC](https://www.ruby-forum.com/t/time-zone-active-record-persistence-error-windows-only/153345 "2008-11-15T06:47:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 15, 2008, 6:47am UTC](https://www.ruby-forum.com/t/time-zone-active-record-persistence-error-windows-only/153345/1 "2008-11-15T06:47:21Z")

</div>

The error below has been encountered using both Rails 2.1.1 and 2.1.2.

I have a strange Time Zone / Active Record persistence error that only  
occurs on Windows (it works fine on OS X). In addition, it only occurs  
when I run my entire test suite (units). If I only run a single test, it  
works fine. And the code works fine when I run my application on either  
operating system.

It appears Active Record is trying to serialize the entire  
TZInfo::DataTimezone object into the database column when my “user”  
object is saved. This results in a “Data too long for column error”.

The time zone field is mapped as a string in the database. I create a  
time\_zone object in my “user” model object as follows:

def time\_zone\_object  
::ActiveSupport::TimeZone[time\_zone]  
end

The error message is:

ActiveRecord::StatementInvalid: Mysql::Error: #22001Data too long for  
column ‘time\_zone’ at row 1:   
!ruby/object:ActiveSupport::TimeZone \nname: Central Time (US &  
Canada)\ntzinfo: !ruby/object:TZInfo::DataTimezone \n info:  
!ruby/object:TZInfo::DataTimezoneInfo \n identifier:  
America/Chicago\n last\_month: 11\n last\_year: 2050\n offsets:  
\n :o4: &id005 !ruby/object:TZInfo::TimezoneOffsetInfo \n  
abbreviation: :CWT\n std\_offset: 3600\n utc\_offset:  
-21600\n utc\_total\_

Attached is the entire error message. This is really strange and any  
tips on a workaround would be greatly appreciated. Here is the unit test  
code that is failing:

require File.dirname( **FILE** ) + ‘/…/test\_helper’  
require “active\_support/values/time\_zone”

class UserTest \< Test::Unit::TestCase  
fixtures :users, :teams, :seasons, :countries

def setup  
delete\_all\_users  
@usa = countries(:us)  
@central = ::ActiveSupport::TimeZone[“Central Time (US & Canada)”]  
end

def test\_active?  
bob = User.new(:name =\> Name.new(‘bob’, nil, ‘smith’),  
:email =\> ‘removed\_email\_address@domain.invalid’,  
:password =\> ‘secret’,  
:password\_confirmation =\> ‘secret’,  
:time\_zone =\> @central,  
:country\_flag =\> @usa)  
bob.admin = true  
bob.save!

```
assert !bob.active?

bob.teams << Team.new(:name => "bob's team", :season =>

```

Season.current)  
bob.save!  
assert bob.active?  
end

Anyone ever seen anything like this before? Thanks!

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 15, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/time-zone-active-record-persistence-error-windows-only/153345/2 "2008-11-15T18:26:51Z")

</div>

On 15 Nov 2008, at 05:47, John H. wrote:

> operating system.  
> end

If the time\_zone attribute is supposed to be a name like “Central Time  
(US & Canada)” shouldn’t you be trying to set it to such a string  
rather than trying to set it to the actual TimeZone instance?

Fred

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 15, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/time-zone-active-record-persistence-error-windows-only/153345/3 "2008-11-15T23:26:13Z")

</div>

Hi Frederick,

That makes a lot more sense and that’s what I’ve done. I think I was  
expecting too much magic out of Active Record / Rails. I’m still  
getting up to speed on this time zone stuff. I’m not sure why the prior  
approach ever worked at all (on my Mac). There are times where I still  
need an actual TimeZone object, so I’ve added the following method to my  
User class.

def time\_zone\_object  
::ActiveSupport::TimeZone[time\_zone]  
end

And the following if I need to format a time based on the user’s time  
zone.

def tz\_strftime(format, time)  
time\_zone\_object.tzinfo.strftime(format, time)  
end

Problem solved. Thanks again for the help.
