Generieren Sie eine geschachtelte JSON-array in JBuilder

Habe ich diese Modelle in ruby on rails

Branch-Modell: has_many :Menüs

class Branch < ActiveRecord::Base           
  belongs_to :place
  belongs_to :city
  has_many :menus , dependent: :destroy
  belongs_to :type_place
end

Menü Modell: has_many :Produkte

class Menu < ActiveRecord::Base
  attr_accessible :description, :product_name, :price, :category_id, :menu_id
  belongs_to :branch
  has_many :products, dependent: :destroy
end

Produkt-Modell:

class Product < ActiveRecord::Base
 belongs_to :menu
 belongs_to :category
end

mit dem folgenden code in der Ansicht:

if @condition
  json.code :success
  json.branch do
    json.array!(@branches) do |json, branch|
      json.(branch, :id, :branch_name, :barcode)
      json.menu branch.menus, :id, :menu_name
    end
  end
else
  json.code :error
  json.message 'Mensaje de error'
end

bekommt:

{
 "code": "success",
 "branch": [
{
  "id": 1,
  "branch_name": "Sucursal 1",
  "barcode": "zPOuByzEFe",
  "menu": [
    {
      "id": 2,
      "menu_name": "carta sucursal 1"
    }
  ]
},
{
  "id": 2,
  "branch_name": "Sucursal Viña Centro",
  "barcode": "RlwXjAVtfx",
  "menu": [
    {
      "id": 1,
      "menu_name": "carta viña centro"
    },
    {
      "id": 5,
      "menu_name": "carta viña centro vinos"
    }
  ]
},
{
  "id": 3,
  "branch_name": "dddd",
  "barcode": "eSbJqLbsyP",
  "menu": [

   ]
  }
 ]
}

Aber als ich die Produkte erhalten, die in jedem Menü?, Ich vermute, ich muss die Iteration Menü, aber ich habe versucht, mehrere Möglichkeiten, ohne Erfolg.

InformationsquelleAutor FrancoML | 2014-01-06
Schreibe einen Kommentar