Rails 4 export as csv: undefined method `attributes'
<p>I am trying to export data to csv file. My code this</p>

<pre><code>@products = Product.all

    respond_to do |format|
      format.csv { render text: @products.to_csv }
    end
</code></pre>

<p>and in my model</p>

<pre><code>def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv &lt;&lt; column_names
      all.each do |product|
        csv &lt;&lt; self.attributes.values_at(*column_names)
      end
    end
  end
</code></pre>

<p>This is not working. I get this error</p>

<pre><code>NoMethodError in ProductsController#index
undefined method `attributes' for #&lt;Class:0x000000047e91c8&gt;
</code></pre>

<p>I am in Rails 4 and ruby 2.2. How can I fix this?</p>