Transaction blocks and rollbacks

Hello, I’m beginning to test a payment method, here is an abbreviated
form.

How can I rollback and redo the transaction if any of the save calls
fail?

def process_payment! # class Payment
transaction do
success = true

  user.blitz_interest = true
  user.points += 1
  user.blitz_contributes += DIVIDEND

  user.credit.pebbles += 1

  blitz_fund = BlitzFund.find_or_create_by_dues(DIVIDEND)
  blitz_fund.general_pool += DIVIDEND

  save
  user.save
  user.credit.save
  blitz_fund.save
end

end

The simplest way is:

raise ActiveRecord::Rollback

which will roll the whole transaction it’s executed inside of back.

–Matt J.

On Sep 13, 3:19 pm, Jesse C. [email protected]

Hi Jesse C.

Like

ActiveRecord::Base.transaction do
#paste your code here
end

Sijo

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs