user2854544
user2854544

Reputation: 901

Ruby on Rails Sql transactions

I am wondering if there is a better way to manage transactions with Ruby on rails than attaching it to the class model ?

Student.transaction do
  Course.transaction do
    course.enroll(student)
    student.units += course.units
  end
end

I don't want my transactions to be model specific, so that I can begin/commit/rollback them genericly at the controller level with no model dependency.

Upvotes: 0

Views: 383

Answers (1)

KappaNossi
KappaNossi

Reputation: 2706

You can also use ActiveRecord::Base.transaction.

AFAIK there is no difference at all. Opening a transaction with one of your models does not limit the usage of different objects not belonging to this model within the transaction at all.

See here: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

Upvotes: 2

Related Questions