port5432
port5432

Reputation: 6381

Rollback in Rails?

In my Rails application, I have a method which copies many rows, and also goes on to copy down some of the parent-child relationships.

def merge
   params[:merge_rows].each do |merge_row|
      batch_detail = BatchDetail.find(merge_row)
      batch_detail.duplicate
      batch_detail.batch_id = batch.id
      batch_detail.save
   end
   render nothing: true
end

# BatchDetail.duplicate
def duplicate
  batch_detail = dup
  batch_detail.primer3_parameter = primer3_parameter.dup if primer3_parameter.present?
  primer3_outputs.each do |primer3_output|
    batch_detail.primer3_outputs << primer3_output.duplicate
  end
  batch_detail
end

Ideally, I would like to only save if all rows are successfully duplicated, and rollback all changes if any are unsuccessful.

Then I would like to report 200 or 500 via the render if successful or error.

Upvotes: 0

Views: 77

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

wrap your ActiveRecord changes in a transaction block, if the end of the block is bypassed by some exception, all changes are rolled back.

begin
  ActiveRecord::Base.transaction do
    ...various transactions
    if (some_error_condition)
      raise
    end         
  end
  ...stuff to do if all successful
rescue
  ...stuff to do on failure
end

Upvotes: 1

Related Questions