Search code examples
ruby-on-railsjsonrestactiveresource

How should look link the json response so that ActiveResource may generate a DateTime object from a date?


I have a question object with a created_at attribute in it. When I will find a question i get back the created_at from the REST service, but Activeresource do not convert the date string to DateTime.

Question.find(1)
/questions/1.json

Question.find(1).created_at.class
=> String

In which format should be the Json response, so that ActiveResource will convert it to a DateTime?

If it is not possible, what should i do?


Solution

  • According to slide #14 in this presentation, you can do this in config/initializers/active_resource.rb:

    ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
    ActiveSupport.parse_json_times = true