我已经审核了很多很多回复,仍然无法使用csv文件预加载rails 5 table。这是我第一次尝试从rails控制台加载数据。请帮忙!
csv文件格式与CSV.foreach()
相关["model_id", "model_make_id", "model_name", "model_trim", "model_year", "model_make_display_name"]
["44328", "acura", "MDX", nil, "2011", "Acura"]
["44331", "acura", "RDX", nil, "2011", "Acura"]
["44332", "acura", "RDX", "SH-AWD", "2011", "Acura"]
["44334", "acura", "RL", nil, "2011", "Acura"]
["44337", "acura", "TL", nil, "2011", "Acura"]
.rb文件代码
require 'csv'
CSV.foreach('./tmp/cq_sample.csv', :headers => true) do |row|
record = Cqref.new(
:model_id => row[0], # first column of csv file
:model_make_id => row[1], # second column
:model_name => row[2], # third
:model_trim => row[3], # fourth
:model_year => row[4], # fifth
:model_make_display_name => row[5] # sixth
)
record.save
end
模式
create_table "cqrefs", force: :cascade do |t|
t.string "model_id"
t.string "model_make_id"
t.string "model_name"
t.string "model_trim"
t.string "model_year"
t.string "model_make_display_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
错误消息提取:
require './tmp/importcsv'
ActiveRecord::DangerousAttributeError: model_name is defined by Active Record. Check to make sure that you don't have an attribute or method with the same name.
from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods.rb:91:in `instance_method_already_implemented?'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_methods.rb:288:in `block in define_attribute_method'