title: Using jbuilder to access child objects in has many relationship
body: <p>Trying to use jbuilder with gon gem to pass an object and its child objects to javascript, where object is a <code>lender</code> and it has many children, which are <code>inventories</code>. Following instructions from gon gem <a href="https://github.com/gazay/gon/wiki/Usage-with-jbuilder" rel="nofollow noreferrer">here</a>, but I'm maintaining that this is a question on the jbuilder side, not gon... you'll see why.</p>
  
  <p>Current code to fetch lenders:</p>
  
  <p><strong>#/test.json.builder</strong></p>
  
  <pre><code>json.lenders @lenders, :email, :latitude, :longitude
  </code></pre>
  
  <p><strong>Test Action in Controller code</strong></p>
  
  <pre><code>@lenders = Signups.all
  gon.jbuilder
  </code></pre>
  
  <p><strong>#/test.html.erb</strong></p>
  
  <pre><code>&lt;script&gt;
    console.log(gon.lenders)
  &lt;/script&gt;
  </code></pre>
  
  <p>This is working fine, and produces the following output:<img src="https://i.stack.imgur.com/5rOha.png" alt="enter image description here"></p>
  
  <p>However, when I try to fetch the children <code>inventories</code> using standard jbuilder code per Ryan Bates' show <a href="http://railscasts.com/episodes/320-jbuilder" rel="nofollow noreferrer">notes</a>, like so:</p>
  
  <p><strong>#/test.json.builder</strong></p>
  
  <pre><code>json.lenders @lenders, :email, :latitude, :longitude
  json.inventories @lenders.inventories, :id, :itemlist_id, :description
  </code></pre>
  
  <p>I get the error: <code>undefined method</code>inventories' for #`</p>
  
  <p><strong>I get why I'm getting the error, because per output above, the result of <code>@lenders</code> is an array of the <code>lenders</code>, not the lender objects themselves, but then how else do I get the inventories?</strong> My desired goal is that the output looks as above, but that for each object, there is an <code>inventories</code> field which produces an array of the individual <code>inventory</code> objects, each with the attributes <code>id</code>, <code>itemlist_id</code> and <code>description</code>.</p>
  
  <hr>
  
  <p>Things I've tried that have produced same error:</p>
  
  <p><strong>ONE</strong> other variation of getting to child per Ryan Bates</p>
  
  <pre><code>json.comments @article.comments do |json, comment|
    json.partial! comment
  end
  
  #in partial
    json.(comment, :id, :name, :content)
  </code></pre>
  
  <p><strong>TWO</strong> including inventories in controller code</p>
  
  <p><strong>Test Action in Controller code</strong></p>
  
  <pre><code>@lenders = Signups.all.includes(:inventories)
  gon.jbuilder
  </code></pre>
  
  <p>And this is not a gon problem, gon is installed after jbuilder in gemfile, and I am including it in the view header.</p>