legendary_rob
legendary_rob

Reputation: 13012

Ruby/Rails updating existing records with duplicate's data

I have an app that has monitors the development of skills, it has many training_programs and each training_program has many participants.

The user every month can add either participants individually or by upload through excel. I have a situation now where people are uploading a spreadsheet that is amended each week, this is duplicating the participants within the system.

One of the fields is times_trained, and what they are expecting is each time a participant is added if it exists then just sum all the attributes together.

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

So if we want to add

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

Rather than adding a new participant, it should update the existing one. so if we want to find rob again we can say

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

We use the ID as the unique optional identifier, so if they don't have an id then we assume that they are always new. Is there anything baked into rails that i can use to do this?

What i am planning on doing is before saving it i'll find if there is a participant with the id provided, if so then pull that record and sum the two of them together and save the record. ill have to test how efficient that will be, but i don't see it being very efficient.

Any help or reading would be great. I have found a few queries on SO about finding duplicate records but its on a global scale and it can take up to 60seconds just to locate a duplicate never mind updating it. we have close to 25 million participants within the Participant table and 650 thousand are duplicates.

Upvotes: 0

Views: 151

Answers (1)

rderoldan1
rderoldan1

Reputation: 4098

you can use find_or_initialize_by and write something like 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 this row default 0 in your db
participant. amount_spent += excel_row[:name]) #Set this row default 0 in your db
participant.save

So, if a participant is found, the data is updated, in the other case it update the row

Upvotes: 1

Related Questions