Ruzia
Ruzia

Reputation: 192

Declarative transaction management in Rails

I am new in Ruby and Rails. I'd like to use Declarative transaction management like Java's @Transactional annotation in Rails.

Does anyone know how to use "Declarative transaction management" in Rails?

Upvotes: 0

Views: 474

Answers (2)

Andreas
Andreas

Reputation: 348

there is nothing like annotations in ruby, but you could build yourself something similar using meta programming. Remember, private, protected, and public are also simply methods and not keywords. You could build yourself something similar

transactional
def my_transactional_method
  puts "some stuff happens here"
end

maybe this gem https://github.com/fredwu/ruby_decorators helps you with that. Or even better https://github.com/michaelfairley/method_decorators, there is also a sample which implements a Decorator for Transactions.

Upvotes: 1

Manjunath Manoharan
Manjunath Manoharan

Reputation: 4617

I'm not from a java background, but I read about Declarative transaction management. In rails we have

ActiveRecord::Base.transaction do
  #do some model operation
  #do some model operation
end

Here only when both the operations are successful, a commit to the db is performed, If one of them fails, none of the operations will be committed to the db.

Upvotes: 0

Related Questions