title: Flatten JSON data in Rails when using :include
body: <p>I've added an "Advance Search" to my app using the "<a href="http://railscasts.com/episodes/111-advanced-search-form-revised" rel="nofollow">#111 Advanced Search Form (revised)</a>" tutorial and have created an API for it too. Everything works fine, but now I am trying to show the JSON data.</p>
  
  <p>This is the cURL command:</p>
  
  <pre><code>curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X 
  GET http://0.0.0.0:3000/api/searches/129
  </code></pre>
  
  <p>And this is the results of the Curl command (Please note the brackets[ ], therefore this is an array of hashes.):</p>
  
  <pre><code>[{"id":4,"state_id":5,"name":"School Liason Officer","created_at":"2014-10-16T03:13:00.000Z",
  "updated_at":"2014-10-16T03:13:00.000Z","state":{"name":"California"}}]
  </code></pre>
  
  <p>But I would like to change the JSON Results so that the state name is flattened with the JSON data somehow.</p>
  
  <p>Does anyone know how I can do this, or if it's even possible to flatten the data? </p>
  
  <p>This is an example of what I'd like:</p>
  
  <pre><code>[{"id":4,"state":"California","name":"School Liason Officer","created_at":"2014-10-16T03:13:00.000Z",
  "updated_at":"2014-10-16T03:13:00.000Z"}]
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>class State &lt; ActiveRecord::Base  
    has_many :district_resources
  
  end
  
  class DistrictResource &lt; ActiveRecord::Base
    belongs_to :state
  
  end
  
  class Search &lt; ActiveRecord::Base
  
    def district_resources
      @district_resources ||= find_district_resources
    end
  
    def find_district_resources
      district_resources = DistrictResource.order(:name)
      district_resources = district_resources.where(state_id: state_id) if state_id.present?
      district_resources
    end
  
  end
  </code></pre>
  
  <p>API Controller:</p>
  
  <pre><code>module Api
    module V1
      class SearchesController &lt; ApplicationController
  
        respond_to :json
  
        def show
          @search = Search.find(params[:id])
          @searches = []
          (@searches &lt;&lt; @search.district_resources).flatten!
  
          json = @searches.to_json
  
          def flatten_hash(h)
            new_hash = {}
  
            ###I get an error here undefined method `each_pair' for Array
            h.each_pair do |key, val|
              if val.is_a?(Hash)
                new_hash[key] = val.values.join(',')
              else
                new_hash[key] = val
              end
            end
            new_hash
          end
  
          flattened_hash = flatten_hash(JSON.parse(json))
          flattened_hash.delete("state_id")
  
          respond_to do |format|
  
            ###How can i merge the state name with the json results
            format.json { render :json =&gt; flattened_hash.to_json(:include =&gt; [:state =&gt; {:only =&gt; [:name]}] )}
          end
        end
  
      end
    end
  end
  </code></pre>
  
  <p>ERROR</p>
  
  <pre><code>undefined method `each_pair' for #&lt;Array:0x007f8d727b5168&gt;
  </code></pre>