apotonick/roar — Issue #4: Representer Ignored When Object Part of a Data Structure
Author: alexreisner
Created: 2011-12-15 15:28:36+00:00
State: open

I like my API's responses to contain a single root node which describes the contents of the file (like well-formed XML). Let's say I have a Fruit class which uses a FruitRepresenter. In my controller, then, I'd like to do something like this:

render :json => {"fruit" => @fruit}

However, this doesn't work because when `to_json` is called on the hash the built-in Rails serialization (which uses `as_json`) is used, instead of the structure defined in FruitRepresenter. I can fix this by adding the following to FruitRepresenter:

def as_json(options = {})
to_hash
end

but it seems like this should be handled by Roar. What do you think?