浏览架构中两条路径上的关系 [英] Navigating through relations on two paths in schema
问题描述
从上到下,我的表之间都有belongs_to关系,而在另一个方向上具有has_many。
From top to bottom I have the belongs_to relationship between my tables , and well has_many from the other direction.
ReportTarget
Report
Manager
Organization
和
Score
Manager
Organization
,因此请注意 Report
表和 Score
表在同一级别上。他们俩都以 Manager
表作为其父项。
so notice that Report
table and Score
table are kind of on the same level. They both have Manager
Table as their parent.
我个人可以弄清楚如何通过急切的加载对其进行导航。
首先要做的是:
Individually I could figure out how to navigate them with eager loading. For first one I will do:
@blah = Organization.includes(managers: { reports: :report_targets }).find(params[:id])
,第二个我可以做到: / p>
and for the second one I can do this:
@blah = Organization.includes([managers: :scores]).find(params[:id])
但是由于我正在控制器中进行操作,并且想将JSON传递给JBuilder,所以我不知道如何通过他们两个?还是将它们组合在一起?这样产生的散列会将它们放在一个散列中,但具有单独的键:
But because I am doing with in my controller and want to pass the JSON to JBuilder, I don't know how to pass both of them? or maybe combine them together? such that resulting hash would have them in one hash but with separate keys:
{
"firstoneinfo" : [
# stuff that first json returns, can have their own internal hashes
],
"SecondOneinfo : [
#stuff that second json returns, can have their own internal hashes
]
}
推荐答案
使用两个不同的实例变量,
Use two different instance variables,
@firstoneinfo = Organization.includes(managers: { reports: :report_targets }).find(params[:id])
@SecondOneInfo = Organization.includes([managers: :scores]).find(params[:id])
,然后只使用.json.jbuilder视图文件中的那些
and then just use those inside the .json.jbuilder view file
这篇关于浏览架构中两条路径上的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!