Search code examples
ruby-on-railsjbuilder

passing associated object in json.array jbuilder


I have list of appointments in @appointments. I can access corresponding customer of an appointment like this

appointment_object.customer

Now I want to render customer partial file which accepts customer object.

This is jbuilder file which renders partial of customer. Problem here is I have @appointments but I want to send corresponding customer inside this partial.(I dont to change customer partial as I know we can do it inside it also.But this is commong partial and I dont to want to change it)

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

Solution

  • Found solution myself

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