4

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?

2 Answers 2

7

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
0

JSON will always be a string as this is how it passes data as stringyfied objects. If you want to convert it to a date/time object you will need to parse it at the server side.

Time.parse should do the trick

http://ruby-doc.org/stdlib-2.0/libdoc/time/rdoc/Time.html#method-c-parse

1
  • But i need the json structure, because my rest service is in perl not in Rails.
    – Piioo
    Commented Jul 26, 2013 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.