Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

to_yaml fails for DateTime objects

#3511

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

Reported by arash · November 25th, 2009 @ 09:42 PM

State: resolved
Milestone: none
Assigned to: Geoff Buesing Geoff Buesing
Importance: none

Activity

  1. arash
    arash

    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
    

    December 2nd, 2009 @ 10:51 PM

  2. Serguei Filimonov
    Serguei Filimonov
    • 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.

    December 11th, 2009 @ 09:21 PM

  3. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Geoff Buesing

    December 14th, 2009 @ 07:55 PM

  4. Serguei Filimonov
    Serguei Filimonov

    Also attached a patch for the master branch with the same fixes.

    December 14th, 2009 @ 09:21 PM

  5. Geoff Buesing
  6. arash
    arash

    thanks for taking care of this.

    December 17th, 2009 @ 02:54 AM