在 json.array jbuilder 中传递关联对象

passing associated object in json.array jbuilder

我在 @appointments 中有约会列表。 我可以这样访问一个约会的相应客户

appointment_object.customer

现在我要渲染客户部分文件 接受客户对象。

这是呈现部分客户的 jbuilder 文件。这里的问题是我有 @appointments 但我想在此部分中发送相应的客户。(我不 改变客户部分,因为我知道我们可以在里面做 also.But 这是常见的部分 而且我不想改变它)

json.customers do
  json.array! @appointments, partial: 'api/v1/customers/customer', as: :customer
end

自己找到解决方案

json.customers do
  json.array! @appointments do | app |
    json.partial! 'api/v1/customers/customer', customer: app.customer
  end
end