执行 import.rake 上传 csv 文件时发生未知属性错误
Unknown attribute error while executing import.rake to upload csv file
我一直在尝试将 devices.csv 文件中的数据加载到数据库中的设备 table 中。
但是 运行
rake db:devices RAILS_ENV=production
在命令行中的 rake 命令上方,我得到未知属性:错误。
下面是结果轨迹。
** Invoke db:devices (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:devices
rake aborted!
unknown attribute:
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in `initialize'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/oink-0.10.1/lib/oink/instrumentation/active_record.rb:60:in `initialize'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/persistence.rb:44:in `new'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/persistence.rb:44:in `create'
/opt/EDDIE/lib/tasks/import.rake:6:in `block (3 levels) in <top (required)>'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1719:in `each'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1120:in `block in foreach'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1266:in `open'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1119:in `foreach'
/opt/EDDIE/lib/tasks/import.rake:5:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:devices
这是我在 lib/tasks 文件夹中的 rake 文件。
require 'csv'
namespace :db do
desc "Import devices from csv file"
task :devices => [:environment] do
CSV.foreach('devices.csv', :headers => true, :encoding => 'ISO-8859-1:UTF-8') do |row|
Device.create(row.to_hash)
end
end
end
任何人都可以帮助如何忽略此错误并将数据加载到数据库中吗?
我认为您的 header 中有些内容与列名称不匹配。
当像这样导入数据时,如果您不想费心将每个 csv 列映射到模型属性以保证其正常工作,您可以忽略没有匹配 header 的列。
使用你的例子,它可能是这样的:
task :devices => [:environment] do
CSV.foreach('devices.csv', :headers => true, :encoding => 'ISO-8859-1:UTF-8') do |row|
params = row.to_hash.with_indifferent_access.select{ |x| Device.attribute_names.index(x)}
Device.create(params)
end
end
我一直在尝试将 devices.csv 文件中的数据加载到数据库中的设备 table 中。 但是 运行
rake db:devices RAILS_ENV=production
在命令行中的 rake 命令上方,我得到未知属性:错误。
下面是结果轨迹。
** Invoke db:devices (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:devices
rake aborted!
unknown attribute:
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/base.rb:498:in `initialize'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/oink-0.10.1/lib/oink/instrumentation/active_record.rb:60:in `initialize'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/persistence.rb:44:in `new'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/activerecord-3.2.13/lib/active_record/persistence.rb:44:in `create'
/opt/EDDIE/lib/tasks/import.rake:6:in `block (3 levels) in <top (required)>'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1719:in `each'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1120:in `block in foreach'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1266:in `open'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/csv.rb:1119:in `foreach'
/opt/EDDIE/lib/tasks/import.rake:5:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.0.0-p648/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
/usr/local/rvm/gems/ruby-2.0.0-p648/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:devices
这是我在 lib/tasks 文件夹中的 rake 文件。
require 'csv'
namespace :db do
desc "Import devices from csv file"
task :devices => [:environment] do
CSV.foreach('devices.csv', :headers => true, :encoding => 'ISO-8859-1:UTF-8') do |row|
Device.create(row.to_hash)
end
end
end
任何人都可以帮助如何忽略此错误并将数据加载到数据库中吗?
我认为您的 header 中有些内容与列名称不匹配。
当像这样导入数据时,如果您不想费心将每个 csv 列映射到模型属性以保证其正常工作,您可以忽略没有匹配 header 的列。
使用你的例子,它可能是这样的:
task :devices => [:environment] do
CSV.foreach('devices.csv', :headers => true, :encoding => 'ISO-8859-1:UTF-8') do |row|
params = row.to_hash.with_indifferent_access.select{ |x| Device.attribute_names.index(x)}
Device.create(params)
end
end