XML Serialization is not including milliseconds in datetime field from Rails model

Posted by revgum on Stack Overflow See other posts from Stack Overflow or by revgum
Published on 2010-03-08T18:44:20Z Indexed on 2010/03/08 19:21 UTC
Read the original article Hit count: 865

By default, the datetime field from the database is being converted and stripping off the milliseconds:

some_datetime => "2009-11-11T02:19:36Z"

attribute_before_type_cast('some_datetime') => "2009-11-11 02:19:36.145"

If I try to overrride the accessor for this attribute like;

def some_datetime
  attribute_before_type_cast('some_datetime')
end

when I try "to_xml" for that model, I get the following error:

NoMethodError (undefined method `xmlschema' for "2009-11-11 02:19:36.145":String):

I have tried to parse the String to a Time object but can't get one to include the milliseconds;

  def some_datetime
    Time.parse(attribute_before_type_cast('some_datetime').sub(/\s/,"T").sub(/$/,"Z"))
  end

Can anyone help get get a datetime with milliseconds rendered by to_xml?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord