title: Rails - Export CSV not working as expected (undefined method)
body: <p>I am doing a CSV Export in Rails App.
  The data is returned as a JSON array from the backend API. 
  But now I am getting error as <code>"undefined method  'to_csv' for #Hash:0x007f149a5ff978"</code> in the export_data method.</p>
  
  <p>I have included <code>require "csv"</code> in the config/application.rb file, and did server restarts.</p>
  
  <p>Not sure what I am doing wrong. </p>
  
  <p><strong>index.html.erb</strong></p>
  
  <pre><code>&lt;a href="usage_metrics/export_data.csv"&gt;&lt;button class="btn btn-default"&gt;Export to CSV&lt;/button&gt;&lt;/a&gt;
  </code></pre>
  
  <p><strong>usage_controller(front end)</strong></p>
  
  <pre><code>      def export_data
          rest_resource = RestClient::Resource.new( ENV['USAGE_METRICS_API'] + '/get_events_data', :verify_ssl =&gt; false )
          request = rest_resource.get  :Authorization =&gt; cookies.signed[:remember_token], :content_type =&gt; 'application/json'
          @events_data = JSON.parse(request)
  
  
          respond_to do |format|
               format.html
               format.csv { send_data @events_data.to_csv }
          end
        end   
  
  
        def self.to_csv
          CSV.generate do |csv|
            csv &lt;&lt; column_names
            all.each do |event|
              csv &lt;&lt; event.attributes.values_at(*column_names)
            end
          end
        end
  </code></pre>