Out of date documentation?

The documentation on to_json says that serialization outputs just the attributes hash as json from an AR record

That suggests that if I did something like Account.find(:first).to_json I'd get something like

{"name": ...}

However I'm looking at several version of rails apps (2.2 and edge/2.3) from the console and if I do something like Account.find(:first).to_json I get something more akin to

{"account": {"name" : ...}}

I'm not sure which one is right and which one is wrong.

It's also worth noting that if ActiveResource gets the second type as a response, it actually creates an incorrect Account object where attributes hash contains the proper Account object. ActiveResource appears to require the response listed in the documentation although I haven't seen that being generated.

Just a note, I removed all my JSON gems just in case there was some kind of conflict.

What's going on here, am I just overlooking something?

Josh Ferguson

{"account": {"name" : ...}}

I'm not sure which one is right and which one is wrong.

It's also worth noting that if ActiveResource gets the second type as a response, it actually creates an incorrect Account object where attributes hash contains the proper Account object. ActiveResource appears to require the response listed in the documentation although I haven't seen that being generated.

Just a note, I removed all my JSON gems just in case there was some kind of conflict.

What's going on here, am I just overlooking something?

I believe this is tied up with ActiveRecord::Base.include_root_in_json
(which in a newly generated rails app is set to true by an initializer)

Fred

Is there a reason activeresource doesn't parse the default json output
properly? Is there an option in activeresource there we can set to
match the default output? This was pretty frustrating for me as it
seemed like it should "just work" but doesn't. Also it seems like the
correct thing here is to just have activeresource recognize root
elements properly.

Josh Ferguson

Josh Ferguson wrote:

Is there a reason activeresource doesn't parse the default json output
properly? Is there an option in activeresource there we can set to
match the default output? This was pretty frustrating for me as it
seemed like it should "just work" but doesn't. Also it seems like the
correct thing here is to just have activeresource recognize root
elements properly.

This definitely seems it's worth adding a new lighthouse ticket targetting 2.3. Round tripping is key point with ARes.