我必须遵循 rabl 代码来生成一些 JSON 数据。
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
它生成的数据如下所示:
[
{
"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": []
}
}
]
您可以忽略event
哈希,奇怪的事情发生在 2users
数组的底部。
如您所见,rsvp_users
即使users
我将root
参数设置为"rsvp_users"
. 但是,对于checkedin_users
数组(现在是空的),我不需要做任何事情,它的名字是自动的checkedin_users
。这里发生了什么?它是rabl中的错误吗?还是我错过了什么?