[RUBY] How to update with activerecord-import

I didn't write it subtly, so I made a note for myself

update_users = []
users.each do |user|
  user.name = "John"
  user.email = "[email protected]"
  update_users << user

#Here on_duplicate_key_Pass the column name you want to update to update
User.import update_users, on_duplicate_key_update: [:name, :email]

I understand that the default of Gem is to perform INSERT, and by specifying the column name with the  Ľon_duplicate_key_update` option, the specified column will be updated (Upsert) when the key is covered.

So if you update only the original user and pass it, it will only be updated.

By the way, it seems that the method of specifying this option differs depending on the DB. https://github.com/zdennis/activerecord-import/#duplicate-key-update

# MySQL version
Book.import [book], on_duplicate_key_update: [:title]

# PostgreSQL version
Book.import [book], on_duplicate_key_update: {conflict_target: [:id], columns: [:title]}

Recommended Posts

How to update with activerecord-import
How to update related models with accepts_nested_attributes_for
How to scroll horizontally with ScrollView
[Java] How to update Java on Windows
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
Update MySQL from 5.7 to 8.0 with Docker
How to start Camunda with Docker
How to crop an image with libGDX
How to share files with Docker Toolbox
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
How to deploy
How to do API-based control with cancancan
How to achieve file download with Feign
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to download Oracle JDK 8 rpm with curl
How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
How to use built-in h2db with spring boot
How to search multiple columns with gem ransack
How to use Java framework with AWS Lambda! ??
[Swift] How to link the app with Firebase
How to create multiple pull-down menus with ActiveHash
How to update pre-built files in docker container
How to use Java API with lambda expression
How to get started with Eclipse Micro Profile
How to give your image to someone with docker
How to insert all at once with MyBatis
How to monitor SPA site transitions with WKWebView
How to write test code with Basic authentication
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson
How to get resource files out with spring-boot
How to create member variables with JPA Model
How to verify variable items with WireMock's RequestBodyMatching
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker
How to avoid exceptions with Java's equals method
How to redirect after user login with Spring-security
How to call AmazonSQSAsync
How to use Map
How to write Rails