Activerecord-import getting ArgumentError: Invalid arguments! error

3k views Asked by At

I am trying to import with activerecord import. I am getting Invalid arguments error how can I fix this?

Thanks

My table schema in postgresql:

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

and I am trying to import with following code:

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

I am using postgresql, rails 5.1.

Thanks in advance.

 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'
2

There are 2 answers

0
patkoperwas On BEST ANSWER

If you look at the Documentation for ActiveRecord Import, the method you are using takes an array of columns as the first argument (which you have) and the second argument is an array of arrays, where each array has the values you want to insert.

Your values variable is an array of hashes, so you'll need to convert it like so

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"]]

For each hash in values we call Hash#values - which returns a new array of just the values in the hash.

Or, just use this version of #import which takes a hash.

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
0
garythegoat On

You should be able to modify your code as follows:

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

VehicleLocation.import(locations)

As shown in the docs