Search code examples
ruby-on-railsjrubyactiveresource

RoR ActiveResource self.to_json : Difference between 3.2.1 and 3.0.7


ARes 3.0.7

user = User.find(123)
# response.body outputs {"id":"123","lastName":"Jones","firstName":"Bob"}
user.to_json = {"firstName":"Bob","id":"123","lastName":"Jones"}

ARes 3.2.1

user = User.find(123)
# response.body outputs {"id":"123","lastName":"Jones","firstName":"Bob"}
user.to_json = {"user":{"firstName":"Bob","id":"123","lastName":"Jones"}}

How do I make ARes 3.2.1 behave like ARes 3.0.7? Is there a configuration that touches upon .to_json directly?

Background: I am upgrading from RoR 3.0.7 to RoR 3.2.1. Old environment was JRuby 1.6.2, Ruby 1.8.7. New environment is JRuby 1.6.7, Ruby 1.9.2.


Solution

  • This was changed in rails 3.1 I believe.

    Put the following in your config/application.rb or inside an initializer to set it back.

    ActiveResource::Base.include_root_in_json = true
    

    Please note, you must restart the server for this to take effect.

    update

    Looks like this currently does not modify the output JSON. There is a pull request on Github however. https://github.com/rails/rails/pull/4874