【发布时间】:2017-08-21 12:50:52
【问题描述】:
我正在尝试将 csv 文件导入到 ruby on rails 应用程序数据库。运行bundle exec rake db:import_csv 时出现此错误unknown attribute '2191' for Landslide.。 2191 是 csv 文件第一行的第一项。
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
架构
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
【问题讨论】:
-
能否粘贴您的 csv 文件的标题。
-
如果你这样做
puts row.to_hash会发生什么? -
csv header: Total ID Year_ID Date Date Day number 五天数 year Continent Country Location Type admin_level newlat newlong map space area (m^2) Source 死亡人数 Injures Trigger Notes Notes Report 1 Source 1 Report 2 Source 2 报告 3 来源 3 报告 4 来源 4
-
NoMethodError: private methodputs' 调用 Landslide(调用'Landslide.connection' 建立连接):Class` 当我尝试puts row.to_hash -
如果您使用 row_to 散列您的 csv 标头必须与模型属性匹配
标签: ruby-on-rails csv sqlite sqlite3-ruby