title: Rails import from csv to model
body: <p>I have a csv file with dump data of table and I would like to import it directly into my database using rails.</p>
  
  <p>I am currently having this code:</p>
  
  <pre><code>csv_text = File.read("public/csv_fetch/#{model.table_name}.csv")
  ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{model.table_name}")
  puts "\nUpdating table #{model.table_name}"
  csv = CSV.parse(csv_text, :headers =&gt; true)
  csv.each do |row|
    row = row.to_hash.with_indifferent_access
    ActiveRecord::Base.record_timestamps = false
    model.create!(row.to_hash.symbolize_keys)
  end
  </code></pre>
  
  <p>with help from <a href="https://stackoverflow.com/a/4410880/985051">here</a>..</p>
  
  <p>Consider my Sample  csv:</p>
  
  <pre><code>id,code,created_at,updated_at,hashcode
  10,00001,2012-04-12 06:07:26,2012-04-12 06:07:26,
  2,00002,0000-00-00 00:00:00,0000-00-00 00:00:00,temphashcode
  13,00007,0000-00-00 00:00:00,0000-00-00 00:00:00,temphashcode
  43,00011,0000-00-00 00:00:00,0000-00-00 00:00:00,temphashcode
  5,00012,0000-00-00 00:00:00,0000-00-00 00:00:00,temphashcode
  </code></pre>
  
  <p>But problem with this code is :</p>
  
  <ul>
  <li>It is generating `id' as autoincrement 1,2,3,.. instead of what in 
  csv file.</li>
  <li>The timestamps for records where there is <code>0000-00-00 00:00:00</code> defaults to null automatically and throws error as the column created_at cannot be null...</li>
  </ul>
  
  <p>Is there any way I can do it in <strong>generic way to import from csv to models</strong>?
  or would i have to write custom code for each model to manipulate the attributes in each row manually??</p>