The root name is not valid in rabl and cannot set the child root name

I need to follow the 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, strange stuff happens at the bottom in the 2 users array.

So, as you can see, the rsvp_users child array rsvp_users displayed with the name users , even if I set the root parameter to "rsvp_users" . However, for the checkedin_users array (which is empty now), I do not need to do anything, and this name is automatically checkedin_users . What's going on here? Is this a bug in rabl? Or is that what I am missing?

+6
source share
1 answer

I came across the same exact error, it seems that the problem is related to setting object_root to false. Following Bigxiang's comment, I experimented a bit and found that it works fantastically:

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

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

+7
source

Source: https://habr.com/ru/post/951852/


All Articles