ubuseral
ubuseral

Reputation: 427

Rails: CSV Import

i genereated an csv with columns id, task, description, created_at, updated_at

 create_table "tasks", :force => true do |t|
t.string   "task"
t.string   "description"
t.datetime "created_at",   :null => false
t.datetime "updated_at",   :null => false

I opened the sqlite Konsole

   rails db
   .separator ','
   .C:/User/jkl/Desktop/tasks.csv

I get error datatype mismatch. I dont fill in created_at and updated_at, but this is from rails to null => false.

How do you import?

Upvotes: 0

Views: 485

Answers (1)

Andre Bernardes
Andre Bernardes

Reputation: 1623

I usually do it via a ruby script:

require 'csv'

def import
  CSV.foreach('path/to/file.csv', :col_sep => ';', :headers => true) do |row|
    Task.create!(row.to_hash)
  end
end

This works assuming your CSV headers match your column names, like so:

task;description
"Task 1";"Paint the fence"

Hope that helps!

Upvotes: 3

Related Questions