wrong child root name in rabl and can't set child root name
Asked Answered
D

1

6

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?

Dignity answered 16/8, 2013 at 1:26 Comment(3)
Have you tried "child :rsvp_users => :rsvp_users, :object_root => false" ?Actino
Yes I have, and this in fact gives me an error. But if I do :rsvp_users => :rsvp_users alone without the :object_root => false, it would be fine. But I don't want each object in the array to have a name.Dignity
try this "child {:rsvp_users => :rsvp_users}, {:object_root => false}", it works on my machine.Actino
M
8

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 "{}".

Manvell answered 1/11, 2013 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.