title: How to create CSV file using CSV gem in ruby 1.9.2?
body: <p>I am new to ruby 1.9.2. How to generate CSV file in a single ruby script file?</p>
  
  <p>Here, I wrote a ruby script,</p>
  
  <pre><code>require 'rubygems'
  require 'pg'
  require 'active_record'
  require 'csv'
  
  class AttachEmail
  
  def generate_csv
  begin
  filename = "csvout.csv"
  users = User.all
  users.each do |u|
      products = Product.find(:all,:conditions=&gt;["user_id=?",u.id])
      CSV.open(filename, 'w') do |csv|
      # header row
  user_name = u.name
        csv &lt;&lt; ['Report']
        csv &lt;&lt; ['Name','Product', 'Item Count']
        products.each do |product|
        csv &lt;&lt; [user_name, product.title,product.count]
        end
  end
  end
  rescue Exception =&gt; e
    puts e
  end
  end
  generate= AttachEmail.new
  generate.generate_csv
  </code></pre>
  
  <p>When i run this script.it will produce output like below,</p>
  
  <pre><code>         A                    B        C
  
  0      Report
  1      Name,Product,Item    Count
  2      user1,PD123,10,990   
  </code></pre>
  
  <p>But I need output like, separate column, Please can you kind me ? Thanks in advance</p>