In a rails app I have an action that returns a json representation of a collection of different models. It looks something like this:
respond_to :json def index @cars = Car.all @vans = Van.all respond_with({ :cars => @cars, :vans => @vans }) end
However, I want to customise the attributes and methods that are passed to the json object. A bit like:
respond_with({ :cars => @cars.to_json(:only => [:make, :model], :methods => [:full_name]), :vans => @vans })
Doing the above, causes the json representation of the "cars" to be escaped as one big string, like:
{ "cars":"[{\"car\":{\"make\":\"Ford\" ... etc "vans": [{"van":{"make":"Citreon" ... vans not escaped }
Obviously I'm approaching this the wrong way. Can anyone point me in the right direction?