Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4628

Rails activerecord-import not work with unique key

$
0
0

model

class User < ApplicationRecord  validates :name, uniqueness: trueend

schema

create_table 'users',             force: :cascade,             charset: 'utf8mb4',             collation: 'utf8mb4_0900_ai_ci' do |t|  t.string 'name'  t.timestamps  t.index %w[name], name: 'index_name_on_users'end

problem code (import.rb)

names = ['a','b','c','d']users = []names.each do |name|  users << User.new(    name: name,  )endUser.import(  users,  on_duplicate_key_update: %i[name])

Running import.rb twice saves a,b,c,d twice despite user is having a unique key (8 users saved with name: a,b,c,d,a,b,c,d)

Is there a way to save user records with unique key enabled?(I'm not considering User.find_by & skipping because of performance..)


Viewing all articles
Browse latest Browse all 4628

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>