mysql - Ruby/Rails updating existing records with duplicate's data -


i have app has monitors development of skills, has many training_programs , each training_program has many participants.

the user every month can add either participants individually or upload through excel. have situation people uploading spreadsheet amended each week, duplicating participants within system.

one of fields times_trained, , expecting each time participant added if exists sum attributes together.

training_program.participants.first    => { id: 33, name: "rob", id: 123456789, times_trained: 3, amount_spent: 65000 } 

so if want add

participant.new(name: "rob", id: 123456789, times_trained: 2, amount_spent: 25) 

rather adding new participant, should update existing one. if want find rob again can

participant.find(33)    => { id: 33, name: "rob", id: 123456789, times_trained: 5, amount_spent: 65025} 

we use id unique optional identifier, if don't have id assume new. there baked rails can use this?

what planning on doing before saving i'll find if there participant id provided, if pull record , sum 2 of them , save record. ill have test how efficient be, don't see being efficient.

any or reading great. have found few queries on finding duplicate records on global scale , can take 60seconds locate duplicate never mind updating it. have close 25 million participants within participant table , 650 thousand duplicates.

you can use find_or_initialize_by , write this:

excel_row =  { id: 33, name: "rob", id: 123456789, times_trained: 3, amount_spent: 65000 } participant = participant.find_or_initialize_by(id: excel_row[:id]) participant.name = excel_row[:name] #set row default 0 in db participant. amount_spent += excel_row[:name]) #set row default 0 in db participant.save 

so, if participant found, data updated, in other case update row


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -