title: Rails export to CSV file
body: <p>It is partly working. It generates the file but the output is not how I want it.</p>
  
  <p>Controller</p>
  
  <pre><code>@messages = Message.take(2)
    respond_to do |format|
      format.html
      format.csv { send_data @messages.to_csv }
    end
  </code></pre>
  
  <p>Message.rb</p>
  
  <pre><code>def self.to_csv
      CSV.generate do |csv|
            csv &lt;&lt; Message.attribute_names
            Message.all.each do |message|
              csv &lt;&lt; message.attributes.values
            end
      end
   end
  </code></pre>
  
  <p>I get the CSV file downloaded, it contains the records itself but it does not show the columns and values</p>
  
  <pre><code>#&lt;Message:0x007fca7a028338&gt;,#&lt;Message:0x007fca79a6bf58&gt;
  </code></pre>
  
  <p>I would expect the Message attributes like:</p>
  
  <pre><code>ID,text
  1,hello
  2,world
  </code></pre>