This project is archived and is in readonly mode.

#3511 ✓resolved
arash

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

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>

Pages