0

I'm trying to import a csv file to a ruby on rails app database. I got this error unknown attribute '2191' for Landslide. when running bundle exec rake db:import_csv. 2191 is the first item on the first row of the csv file.

import_csv.rake

require 'csv'
namespace :db do
  task :import_csv => :environment do
    CSV.foreach("Sample_landslides.csv", :headers => true) do |row|
      Landslide.create!(row.to_hash)
    end
  end
end

schema

  create_table "landslides", force: :cascade do |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
Tri Nguyen
  • 1,688
  • 3
  • 18
  • 46
  • can you please paste headers of your csv file. – Dnyanarth lonkar Mar 28 '17 at 12:35
  • What happens if you do `puts row.to_hash`? – Robin van Dijk Mar 28 '17 at 12:50
  • csv header: Total ID Year_ID Date Date Day number Five day number year Continent Country Location Type admin_level newlat newlong mapped spatial area (m^2) Source Fatalities Injuries Trigger Notes Notes Report 1 Source 1 Report 2 Source 2 Report 3 Source 3 Report 4 Source 4 – Tri Nguyen Mar 28 '17 at 12:54
  • `NoMethodError: private method `puts' called for Landslide (call 'Landslide.connection' to establish a connection):Class` when I try `puts row.to_hash` – Tri Nguyen Mar 28 '17 at 12:55
  • if you are using row_to hash your csv headers must match with model attributes – Dnyanarth lonkar Mar 28 '17 at 13:03
  • Thanks it works. Anyway I got this error `ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'Rockfall'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Landslide.inheritance_column to use another column for that information.` and it is able to fetch data till that row. Any idea how to fix it? – Tri Nguyen Mar 28 '17 at 13:17
  • type is restricted word, you can't use it as a column name in ActiveRecord models (unless you're doing STI). please rename attribute type to something else and your problem will get solved. – Dnyanarth lonkar Mar 28 '17 at 13:22
  • @Tri Nguyen Rename type to landslides_type – Dnyanarth lonkar Mar 28 '17 at 13:45
  • I changed it into `landslide_type` both in the csv file and the database schema. Somehow I still got this error `ActiveModel::UnknownAttributeError: unknown attribute 'landslide_type' for Landslide.` – Tri Nguyen Mar 30 '17 at 11:52

1 Answers1

0

As per my knowledge if you are using row_to hash your csv headers must match with 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 do   |row|
  p row.to_hash
end
Dnyanarth lonkar
  • 869
  • 7
  • 12