title: Rails and exporting a console query to csv
body: <p>I have the following query I am running...</p>
  <pre><code>require 'csv'
  require File.dirname(__FILE__) + '/config/environment.rb'
  
  file = &quot;#{Rails.root}/public/data.csv&quot;
  registrations = OnlineCourseRegistration.where(course_class_id: 681).where(status: &quot;Completed&quot;).where(&quot;score &gt;= ?&quot;, &quot;80&quot;).where(&quot;exam_completed_at BETWEEN ? AND ?&quot;, 3.years.ago, Date.today)
   
  CSV.open( file, 'w' ) do |writer|
    writer &lt;&lt; registrations.first.attributes.map { |a,v| a }
    registrations.each do |s|
      if s.user_id
          writer &lt;&lt; User.find(s.user_id).email
      end
      writer &lt;&lt; s.attributes.map { |a,v| v }
    end
  end
  </code></pre>
  <p>This is failing on the line <code> writer &lt;&lt; User.find(s.user_id).email</code> with the error:</p>
  <pre><code>`&lt;&lt;': undefined method `map' for &quot;my_user@yahoo.com&quot;:String (NoMethodError)`
  </code></pre>
  <p>Basically, I just want to add a column with the users email address in it.</p>
  <h1>Update</h1>
  <p>Here is the current output w/out the email field</p>
  <pre><code>id  cart_id user_id course_class_id created_at  updated_at  exam_attempts   exam_completed_at   evaluation_completed_at status  score   add_extension   retest_cart_id  retest_purchased_at
  11990   10278   6073    681 2014-10-30 20:34:18 UTC 2014-12-17 14:48:39 UTC 2   2014-12-17 03:16:44 UTC 2014-12-17 14:48:39 UTC Completed   90  FALSE       
  11931   10178   6023    681 2014-09-02 22:35:08 UTC 2015-02-24 03:58:03 UTC 1   2015-02-24 03:56:12 UTC 2015-02-24 03:58:03 UTC Completed   80  FALSE       
  12015   10316   6089    681 2014-11-15 14:31:05 UTC 2014-11-18 20:14:13 UTC 1   2014-11-18 20:11:46 UTC 2014-11-18 20:14:13 UTC Completed   82  FALSE       
  12044   10358   6103    681 2014-12-03 15:56:39 UTC 2014-12-06 23:05:18 UTC 2   2014-12-06 23:02:13 UTC 2014-12-06 23:05:18 UTC Completed   94  FALSE       
  </code></pre>
  <p>So I would like to append an email field to each of the colums above.</p>