Activerecord-import getting ArgumentError: Invalid arguments! error

Activerecord-import getting ArgumentError: Invalid arguments! error

我正在尝试使用 activerecord 导入进行导入。我收到无效参数错误,我该如何解决?

谢谢

我在 postgresql 中的 table 模式:

create_table "vehicle_locations", force: :cascade do |t|
t.string "plate"
t.datetime "timestamp"
t.float "latitude"
t.float "longitude"
t.integer "speed"
t.integer "mileage"
t.boolean "ignition_on"
t.string "address"
end

我正在尝试使用以下代码导入:

values = [{"plate":"xxx","timestamp":"2017-11-28 
 20:42:57","latitude":41.9322,"longitude":27.3253,"speed":0,"mileage":1984
 04,"ignition_on":true,"address":"address"},{"plate":"xxx","timestamp":"2017-11-28 20:43:46","latitude":47.0148,"longitude":39.1805,"speed":16,"mileage":209726,"ignition_on":true,"address":"address"}]

   columns = [:plate, :timestamp, :latitude, :longitude, :speed, :mileage, :ignition_on,  :address ]
  VehicleLocation.import columns, values

我正在使用 postgresql,rails5.1.

提前致谢。

 2017-11-28T17:45:08.844Z 17976 TID-gn4tnwlkc WARN: ArgumentError: Invalid arguments!
2017-11-28T17:45:08.844Z 17976 TID-gn4tnwlkc WARN: /home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activerecord-import-0.21.0/lib/activerecord-import/import.rb:508:in `import_helper'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activerecord-import-0.21.0/lib/activerecord-import/import.rb:399:in `import'
/home/kayayan/Projects/RoR/trasportapp/app/workers/trio_worker.rb:23:in `perform'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:188:in `execute_job'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:170:in `block (2 levels) in process'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/middleware/chain.rb:128:in `block in invoke'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/middleware/chain.rb:133:in `invoke'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:169:in `block in process'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:141:in `block (6 levels) in dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/job_retry.rb:97:in `local'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:140:in `block (5 levels) in dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/rails.rb:42:in `block in call'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/execution_wrapper.rb:85:in `wrap'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/reloader.rb:68:in `block in wrap'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/execution_wrapper.rb:85:in `wrap'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activesupport-5.1.4/lib/active_support/reloader.rb:67:in `wrap'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/rails.rb:41:in `call'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:136:in `block (4 levels) in dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:204:in `stats'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:131:in `block (3 levels) in dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/job_logger.rb:7:in `call'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:130:in `block (2 levels) in dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/job_retry.rb:72:in `global'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:129:in `block in dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/logging.rb:44:in `with_context'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/logging.rb:38:in `with_job_hash_context'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:128:in `dispatch'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:168:in `process'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:85:in `process_one'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/processor.rb:73:in `run'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/util.rb:16:in `watchdog'
/home/kayayan/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sidekiq-5.0.5/lib/sidekiq/util.rb:25:in `block in safe_thread'

如果您查看 Documentation for ActiveRecord Import,您使用的方法将一个列数组作为第一个参数(您有),第二个参数是一个数组数组,其中每个数组都有您要插入的值。

您的 values 变量是一个散列数组,因此您需要像这样转换它

values = values.map(&:values)
puts values
#=> [["xxx", "2017-11-28 20:42:57", 41.9322, 27.3253, 0, 198404, true, "address"], ["xxx", "2017-11-28 20:43:46", 47.0148, 39.1805, 16, 209726, true, "address"]]

对于 values 中的每个散列,我们调用 Hash#values - returns 一个仅包含散列中值的新数组。

或者,只需使用 this version of #import 即可。

values = [{"plate":"xxx","timestamp":"2017-11-28 
 20:42:57","latitude":41.9322,"longitude":27.3253,"speed":0,"mileage":1984
 04,"ignition_on":true,"address":"address"},{"plate":"xxx","timestamp":"2017-11-28 20:43:46","latitude":47.0148,"longitude":39.1805,"speed":16,"mileage":209726,"ignition_on":true,"address":"address"}]

VehicleLocation.import values

您应该可以按如下方式修改您的代码:

locations = values.map do |attrs|
  VehicleLocation.new(attrs)
end

VehicleLocation.import(locations)

如图the docs