title: Set character encoding when generating CSV file Rails 4.2
body: <p>I have a module to generate csv file of the model when included.</p>
  
  <pre><code># app/models/concerns/csv_exportable.rb
  module CsvExportable
    extend ActiveSupport::Concern
  
    included do
      def self.to_csv(options = {})
        CSV.generate(options) do |csv|
          csv &lt;&lt; column_names
          all.each do |thismodel|
            csv &lt;&lt; thismodel.attributes.values_at(*column_names)
          end
        end
      end
    end
  end
  </code></pre>
  
  <p>and I include the module in models for which  I want to generate csv file</p>
  
  <pre><code># app/models/product.rb
  class Reason &lt; ActiveRecord::Base
    include CsvExportable
    .
    .
  end
  </code></pre>
  
  <p>In my controller I do</p>
  
  <pre><code>respond_to do |format|
    format.html
    format.csv { send_data @products.to_csv, filename: 'products.csv' }
  end
  </code></pre>
  
  <p>I want to set the character encoding of generated csv file to <a href="https://en.wikipedia.org/wiki/Shift_JIS" rel="nofollow">Shift_JIS</a> instead of default utf-8. What is the best way to do this ? Thank you<br />
  <br><strong>EDIT:</strong><br></p>
  
  <p>I tried</p>
  
  <pre><code>format.csv { send_data (@products.to_csv).encode("SHIFT-JIS"), filename: 'products.csv' }
  </code></pre>
  
  <p>now I am getting an error <code>Encoding::ConverterNotFoundError   code converter not found (UTF-8 to SHIFT-JIS)</code></p>