title: Ruby-Rails Exporting search results table to CSV
body: <p>I'm using the CSV class to do result exporting to csv. I have it working now, but it only exports my search query to CSV, not the results (which are displayed on my 'show; page).</p>
  
  <blockquote>
    <p>search.rb</p>
  </blockquote>
  
  <pre><code>class Search &lt; ActiveRecord::Base
  
   def to_xls(options = {})
    {
        "Id" =&gt; id.to_s,
        "firstname" =&gt; firstname,
        "surname" =&gt; surname,
        "officenumber" =&gt; officenumber,
        "department" =&gt; department,
        "division" =&gt; division,
        "address" =&gt; address,
        "notes" =&gt; notes
    }
   end
  end
  </code></pre>
  
  <blockquote>
    <p>searches_controller.rb</p>
  </blockquote>
  
  <pre><code>def show
      @search = Search.find(params[:id])
  
      respond_to do |format|
          format.html
          format.xls { send_data @searches.to_xls, content_type: 'application/vnd.ms-excel', filename: 'search.xls' }
      end
  end
  </code></pre>
  
  <blockquote>
    <p>mime_types.rb</p>
  </blockquote>
  
  <pre><code>Mime::Type.register "application/vnd.ms-excel", :xls
  </code></pre>