0

从上到下,我的表之间有 belongs_to 关系,而从另一个方向来看,还有 has_many 关系。

ReportTarget
Report  
Manager
Organization

Score
Manager
Organization

所以请注意Reporttable 和Scoretable 在同一级别上。他们都有ManagerTable 作为他们的父母。

我个人可以弄清楚如何通过急切加载来导航它们。对于第一个我会做:

@blah = Organization.includes(managers: { reports: :report_targets }).find(params[:id])

对于第二个,我可以这样做:

@blah = Organization.includes([managers: :scores]).find(params[:id])

但是因为我在我的控制器中使用并想将 JSON 传递给 JBuilder,所以我不知道如何传递它们?或者也许将它们结合在一起?这样生成的散列将它们放在一个散列中,但具有单独的键:

{
  "firstoneinfo" : [
     # stuff that first json returns, can have their own internal hashes
     ],
   "SecondOneinfo : [
    #stuff that second json returns, can have their own internal hashes
    ]
}
4

1 回答 1

1

使用两个不同的实例变量,

@firstoneinfo =  Organization.includes(managers: { reports: :report_targets }).find(params[:id])
@SecondOneInfo = Organization.includes([managers: :scores]).find(params[:id])

然后只使用 .json.jbuilder 视图文件中的那些

于 2013-03-01T19:42:38.890 回答