Q:

Using first_or_create in Sidekiq Job

I have a Sidekiq Job with first_or_create.
 class JobWithFirstOrCreate
    analysis = Analysis.where(standard_id: standard_id).first_or_create
    analysis.save!
 end

But this causes uniqueness validation to fail due to duplicate analysis records being created. I understand this is because first_or_create is not atomic.
So what is the right way of doing this?
I want to run jobs concurrently in which I update if an analysis record is already present else insert. 

A:

Look into the term "upsert".  ActiveRecord can't do it natively but you can roll the SQL by hand.

