Undefined method `add_column_for_on_duplicate_key_update` - activerecord-import

809 views Asked by At

I'm trying to use activerecord-import as a way to import multiple records.

I'm trying to run

Field.import records, :on_duplicate_key_update => [:application_id,:key]

where records is an array of Field records.

The error I'm getting is:

NoMethodError: undefined method 'add_column_for_on_duplicate_key_update' for # from /GEMS/gems/activerecord-import-1.0.4/lib/activerecord-import/import.rb:1003:in 'block in add_special_rails_stamps'

I've also tried running Field.import records and Field.import records, on_duplicate_key_update: [:application_id, :key] but both of them give the same error.

I've had a good search but I can't find anything of use anywhere else.

2

There are 2 answers

1
Bodh1004 On

If You are Using Rails 6 then you can use:

Field.insert_all records

If not then try to use following syntax:

Field.import columns, [values]
0
code_aks On

Duplicate Key Update(on_duplicate_key_update)

MySQL, PostgreSQL (9.5+), and SQLite (3.24.0+) support on duplicate key update (also known as "upsert") which allows you to specify fields whose values should be updated if a primary or unique key constraint is violated.

Firstly check your version accordingly, for more reference read this https://github.com/zdennis/activerecord-import