Ruby on rails Unknow attribute when importing csv file to database -
i'm trying import csv file ruby on rails app database. got error unknown attribute '2191' landslide.
when running bundle exec rake db:import_csv
. 2191 first item on first row of csv file.
import_csv.rake
require 'csv' namespace :db task :import_csv => :environment csv.foreach("sample_landslides.csv", :headers => true) |row| landslide.create!(row.to_hash) end end end
schema
create_table "landslides", force: :cascade |t| t.integer "total_id" t.integer "year_id" t.date "start_date" t.date "end_date" t.integer "day_number" t.string "continent" t.string "country" t.string "location" t.string "type" t.integer "admin_level" t.float "lat" t.float "lng" t.boolean "mapped" t.float "spatial_area" t.integer "fatalities" t.integer "injuries" t.string "notes" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "trigger" end
as per knowledge if using row_to hash csv headers must match model attributes.
require "csv" header_map = { "total" => :total_id, } header_converter = ->(header) { header_map.fetch(header, header).to_sym } csv = <<end column1,column2,ext:mat:pidtc 100,200,300 400,500,600 end csv.new(csv, headers: true, header_converters: header_converter).each |row| p row.to_hash end
Comments
Post a Comment