[英]Constructing a nested JSON request with Jbuilder
Currently my JSON request is returning the below, where each person/ lender
has many inventories
. 目前,我的JSON请求返回以下内容,其中每个人/
lender
都有许多inventories
。
#output of /test.json
[
{"id":13, "email":"johndoe@example.com", "inventories":
[
{"id":10,"name":"2-Person Tent","category":"Camping"},
{"id":11,"name":"Sleeping bag","category":"Camping"},
{"id":27,"name":"6-Person Tent","category":"Camping"}
]
},
{"id":14, "email":"janedoe@example.com", "inventories":
[
{"id":30,"name":"Electric drill","category":"Tools"},
{"id":1,"name":"Hammer","category":"Tools"},
{"id":37,"name":"Plane","category":"Tools"}
]
}
]
I need to nest in one more thing and am having trouble doing so. 我需要再嵌套一件事,但这样做有麻烦。 For context, each
inventory
item is referenced via it's id as a foreign key in a borrow
record. 对于上下文,每个
inventory
物料都通过其ID作为borrow
记录中的外键进行引用。 Each borrow
record belongs to a request
parent that stores returndate
and pickupdate
. 每个
borrow
记录都属于一个request
父级,该父级存储了returndate
和pickupdate
。 What I need now, is for each inventory
item, to nest an array of all the request
records, with information on pickupdate
and returndate
. 我现在需要的是,对于每个
inventory
项目,嵌套所有request
记录的数组,以及关于pickupdate
和returndate
。 In other words, desired output: 换句话说,所需的输出:
[
{"id":13, "email":"johndoe@example.com", "inventories":
[
{"id":10,"name":"2-Person Tent","category":"Camping", "requests":
[
{"id":1, "pickupdate":"2014-07-07","returndate":"2014-07-10"},
{"id":2, "pickupdate":"2014-06-02","returndate":"2014-06-05"},
{"id":3, "pickupdate":"2014-08-14","returndate":"2014-08-20"}
]
},
{"id":11,"name":"Sleeping bag","category":"Camping", "requests":
[
{"id":4, "pickupdate":"2014-05-27","returndate":"2014-05-30"},
{"id":5, "pickupdate":"2014-04-22","returndate":"2014-04-25"}
]
},
{"id":27,"name":"6-Person Tent","category":"Camping", "requests":
[
{"id":6, "pickupdate":"2014-07-10","returndate":"2014-07-12"}
]
}
]
},
{"id":14, "email":"janedoe@example.com", "inventories":
...
I have written the following code: 我写了以下代码:
json.array!(@lenders) do |json, lender|
json.(lender, :id, :email)
json.inventories lender.inventories do |json, inventory|
json.id inventory.id
json.name Itemlist.find_by_id(inventory.itemlist_id).name
#code below says, json.requests should equal all the Requests where there is a Borrows within that Request that is using the Inventory in question
json.requests Request.select { |r| r.borrows.select { |b| b.inventory_id == inventory.id }.present? } do |json, request|
json.pickupdate request.pickupdate
json.returndate request.returndate
end
end
end
When I refresh the page, I get wrong number of arguments (0 for 2..5)
刷新页面时,我得到了
wrong number of arguments (0 for 2..5)
I feel like the issue is that the Request.select...
is returning an Array which isn't what needs to go here... but in the earlier nested function lender.inventories
is an Inventory::ActiveRecord_Associations_CollectionProxy
though I'm not sure how to correct for this. 我觉得问题在于
Request.select...
返回的数组不需要在这里...但是在较早的嵌套函数lender.inventories
是Inventory::ActiveRecord_Associations_CollectionProxy
尽管我不是确定如何对此进行更正。
NOTE: Someone said the problem could be that unlike with the nesting between inventories
and lender
, there's not an explicit association between inventory
and request
, but then again the line json.name Itemlist.find_by_id(inventory.itemlist_id).name
worked so I'm not sure this is right. 注意:有人说问题可能在于,与
inventories
和lender
之间的嵌套不同, inventory
和request
之间没有明确的关联,但是行json.name Itemlist.find_by_id(inventory.itemlist_id).name
再次起作用,所以我我不确定这是对的。 (Also if this is the case, I'm not sure how to bypass this limitation... I currently don't want to create a relationship between the two.) (如果是这种情况,我不确定如何绕过此限制……我目前不想在两者之间建立关系。)
Thanks! 谢谢!
ARG. ARG。 Ok so this code is perfectly right.
好的,所以这段代码是完全正确的。 The issue was that I"m using the Gon gem in conjunction with Jbuilder, and Request is a predefined class in Gon.
问题是我将Gon gem与Jbuilder结合使用,而Request是Gon中的预定义类。
So just changed code to 所以只是将代码更改为
@requestrecords.select....
And in the controller: 并在控制器中:
@requestrecords = Request.all
-__- -__-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.