title: Generate a nested JSON array in JBuilder
body: <p>I have this models in ruby on rails</p>
  
  <p>Branch model: has_many :menus</p>
  
  <pre><code>class Branch &lt; ActiveRecord::Base           
    belongs_to :place
    belongs_to :city
    has_many :menus , dependent: :destroy
    belongs_to :type_place
  end
  </code></pre>
  
  <p>Menu  model: has_many :products</p>
  
  <pre><code>class Menu &lt; ActiveRecord::Base
    attr_accessible :description, :product_name, :price, :category_id, :menu_id
    belongs_to :branch
    has_many :products, dependent: :destroy
  end
  </code></pre>
  
  <p>Product model:</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
   belongs_to :menu
   belongs_to :category
  end
  </code></pre>
  
  <p>with the following code in the view:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>gets:</p>
  
  <pre><code>{
   "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": [
  
     ]
    }
   ]
  }
  </code></pre>
  
  <p>But as I get the products of each menu?, I suspect I need to iterate menu, but I have tried several ways without success. </p>