Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrong child root name in rabl and can't set child root name

I have to following rabl code to generate some JSON data.

object @event
attributes :id, :EID, :name, :address, :description, :latitude, :longitude, :time, :created_at
node(:rsvp_count) { |event| event.rsvp_users.count }
node(:check_in_count) { |event| event.checkedin_users.count }
node(:FID) { |event| event.creater.FID if event.creater}

child :rsvp_users, :object_root => false do
    extends 'users/index'
end

child :checkedin_users, :object_root => false do
    extends 'users/index'
end

And the data it generates looks like this:

[
    {
        "event": {
            "id": 2,
            "EID": 123458,
            "name": "event no.2",
            "address": "189 elm st",
            "description": "awesome event",
            "latitude": 10,
            "longitude": 10,
            "time": "2013-10-20T18:00:00Z",
            "created_at": "2013-08-15T21:06:21Z",
            "rsvp_count": 3,
            "check_in_count": 0,
            "FID": 12345678,
            "users": [
                {
                    "id": 4,
                    "FID": 112233445,
                    "name": "name1",
                    "using_app": true
                },
                {
                    "id": 3,
                    "FID": 9999,
                    "name": "name2",
                    "using_app": false
                },
                {
                    "id": 2,
                    "FID": 123456789,
                    "name": "name3-robot",
                    "using_app": true
                }
            ],
            "checkedin_users": []
        }
    }
]

You can ignore the event hash, the weird stuff is happening at the bottom in the 2 users array.

So as you can see, the child rsvp_users array is showing up with the name users even if I set the root param to "rsvp_users". However, for checkedin_users array (which is empty right now), I don't need to do anything, and it's name is automatically checkedin_users. What is happening here? Is it a bug in rabl? Or is it something that I am missing?

like image 371
Enzo Avatar asked Aug 16 '13 01:08

Enzo


1 Answers

I've encountered the same exact bug, the problem seems to be setting the object_root to false. Following the comment of Bigxiang I have experimented a bit and found that this works fantastically:

child( {:rsvp => :rsvp}, {:object_root => false} ) do
   extends "users/index"
end

Note both the round parentheses "()" and braces "{}".

like image 164
Diego Avatar answered Oct 21 '22 15:10

Diego