title: Using jbuilder in rails to construct an array of objects with different classes
body: <p>A model(FirstModel) in my rails app has a has_many relationship with another model(Item).</p>
  
  <pre><code>class FirstModel &lt; ActiveRecord::Base
  
    has_many :items, :dependent =&gt; :destroy
  
    def item_array
      a = []
      self.items.each do |item|
        a.push(item.item_thing)
      end
      a
    end
  end
  </code></pre>
  
  <p>Item itself belongs :item_thing through a polymorphic association.  A method on FirstModel returns an array of "items", not the join model object(which is of class Item), but the item_thing of each Item object, and therefore it is an array of objects of different classes (checked in rails console, and the method returns the array just fine)</p>
  
  <pre><code>class Item &lt; ActiveRecord::Base
    belongs_to :item_thing,  :polymorphic =&gt; true
  
  
  end
  </code></pre>
  
  <p>In my show.json.jbuilder for FirstModel, I want to take that array of "items" and use different templates based on which class the item belongs to.  The following file path is "views/api/items/show.jbuilder"</p>
  
  <pre><code>json.extract! @item, :name
  
  json.items @item.item_array do |item|
    if item.class == 'Book'
      json.partial! 'api/books/show', item
    end
    if item.class == 'Car'
      json.partial! 'api/cars/show', item
    end
  end
  </code></pre>
  
  <p>The car and book template paths are "views/api/cars/show.json.jbuilder" and "views/api/books/show.json.jbuilder.</p>
  
  <p>When I get the json from the above template the name of the FirstModel object is visible, items array is empty, whereas I am trying to get an array of hashes for each item, rendered through its respective template.</p>
  
  <p>Thank you for helping!</p>