davhab
davhab

Reputation: 805

How to ensure atomicity with multiple transactions/updates in Rails?

If I have something like:

Model1.update_all ['a1 = ?, a2 = ?', v1, v2], ['id = ?', id1]
Model2.update_all  ['a3 = ?', v3], ['id = ?', id2]

How can I ensure that if one of the above updates fails that none of them has an effect, ie. a kind of rollback is performed?

Upvotes: 0

Views: 237

Answers (1)

Carson Cole
Carson Cole

Reputation: 4461

This should do it..

Model1.transaction do
  Model1.update_all ['a1 = ?, a2 = ?', v1, v2], ['id = ?', id1]
  Model2.update_all  ['a3 = ?', v3], ['id = ?', id2]
end

Upvotes: 2

Related Questions