timpone
timpone

Reputation: 19969

How to have two pieces of data output as json in rabl

New to rabl and not sure how to do this with two different arrays returned in a single hash like this:

@data={:locations => [location1, location2], :items => [item1,item2]}

In my rabl file, I'd like to do something like the following:

@data[:locations]
extends "api/location_show"
@data[:items]
extends "api/item_show"

to output this:

{
    "locations": [
        {
            "id": 156,
            "name": "Location 1"
        },
        {
            "id": 158,
            "name": "Location 2"
        }
    ],
    "items": [
        {
            "global_id": 3189,
            "header": "pistachio 1"
        },
        {
            "global_id": 3189,
            "header": "pistachio 2"
        }
    ]
}

but it just doesn't seem to be working. Is there a way to get this to work?

thx

Upvotes: 0

Views: 110

Answers (1)

niiru
niiru

Reputation: 5112

Your rabl file should look something like:

object false
child (:locations) { attributes :id, :name }
child (:items) { attributes :global_id, :header }

By setting object to false, you essentially tell rabl that you want to construct your nodes on your own. Then you can go ahead and invoke the child and node methods as you wish.

Upvotes: 1

Related Questions