This project is archived and is in readonly mode.
to_yaml fails for DateTime objects
Reported by arash | November 25th, 2009 @ 09:42 PM
It seems that if an ActiveRecord object contains a DateTime, to_yaml errors out:
Loading development environment (Rails 2.3.4)
>> y = YamlExample.new(:my_date => DateTime.now)
=> #<YamlExample id: nil, my_date: "2009-11-25 21:33:18", created_at: nil, updated_at: nil>
>> y.to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<yaml-example>\n <created-at type=\"datetime\" nil=\"true\"></created-at>\n <my-date type=\"datetime\">2009-11-25T21:33:18Z</my-date>\n <updated-at type=\"datetime\" nil=\"true\"></updated-at>\n</yaml-example>\n"
>> y.to_yaml
NoMethodError: undefined method `to_i' for Wed, 25 Nov 2009 21:33:18 +0000:DateTime
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/time_with_zone.rb:262:in `hash'
from /usr/local/lib/ruby/1.8/yaml.rb:389:in `quick_emit'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:38:in `to_yaml'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:18:in `node_export'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:18:in `add'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:18:in `to_yaml'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:17:in `each'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:17:in `to_yaml'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:16:in `map'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:16:in `to_yaml'
from /usr/local/lib/ruby/1.8/yaml.rb:391:in `call'
from /usr/local/lib/ruby/1.8/yaml.rb:391:in `emit'
from /usr/local/lib/ruby/1.8/yaml.rb:391:in `quick_emit'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:15:in `to_yaml'
from (irb):3
Seems like DateTime doesn't respond to to_i
Comments and changes to this ticket
-
arash December 2nd, 2009 @ 10:51 PM
Didn't have time to dig into the TimeWithZone source, so I am temporarily using this patch:
module ActiveSupport class TimeWithZone def datetime_to_i utc.is_a?(DateTime) ? utc.to_f : utc.to_i end alias_method :to_i, :datetime_to_i alias_method :hash, :datetime_to_i alias_method :tv_sec, :datetime_to_i end end
-
Serguei Filimonov December 11th, 2009 @ 09:21 PM
- Tag changed from activerecord, yaml to activerecord, activesupport, patch, tested, yaml
Turns out the problem is more with the activesupport than with activerecord. Calling to_yaml on TimeWithZone calls #to_i on the class it's wrapping, which could be Time or DateTime. ActiveSupport, which provides the #to_f method, does not provide a #to_i on DateTime. The attached patch introduces the #to_i method for DateTime and is covered with the necessary tests on both activesupport and activerecord.
-
Michael Koziarski December 14th, 2009 @ 07:55 PM
- Assigned user set to Geoff Buesing
-
Serguei Filimonov December 14th, 2009 @ 09:21 PM
Also attached a patch for the master branch with the same fixes.
-
Geoff Buesing December 15th, 2009 @ 02:45 PM
- State changed from new to resolved
Applied in master and 2-3-stable. Thanks!
master: http://github.com/rails/rails/commit/2ae8300489aaaaaa65f50f3557090b...
2-3-stable: http://github.com/rails/rails/commit/754bbaaf37f21f51b345f88ca42b42...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>