# Transaction() method in rails?

**URL:** <https://www.ruby-forum.com/t/transaction-method-in-rails/208529>\
**Category:** Rails\
**Created:** [June 20, 2011, 9:17pm UTC](https://www.ruby-forum.com/t/transaction-method-in-rails/208529 "2011-06-20T21:17:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 20, 2011, 9:17pm UTC](https://www.ruby-forum.com/t/transaction-method-in-rails/208529/1 "2011-06-20T21:17:25Z")

</div>

Let’s say I have this:

test “assign the same transaction id to all memos created in the block”  
do  
school = site = nil  
Memo.group do  
school = make :school  
site = make :site  
end  
assert\_not\_nil school.memos.last.transaction\_id  
assert\_not\_nil site.memos.last.transaction\_id  
assert\_equal school.memos.last.transaction\_id,  
site.audit\_memos.last.transaction\_id  
end

#helper  
def make(factory\_name, \*args, &block)  
factory\_name = factory\_name.model\_name.singular if Class ===  
factory\_name  
options = args.extract\_options!  
count = args.first || 1  
records = []  
count.times do  
record = case factory\_name  
when :user then Factory.custom(factory\_name, options, &block)  
else Factory(factory\_name, options, &block)  
end  
records \<\< record  
end  
records.many?? records : records.first  
end

#memo  
def self.group(&block)  
self.current\_transaction\_id = maximum(:transaction\_id) ?  
maximum(:transaction\_id) + 1 : 100000  
transaction { yield }  
ensure  
self.current\_transaction\_id = nil  
end

So basically when the test is run, make() in the block gets executed,  
passing a key that is associated with a Factory (factory girl). So at  
some point factory\_name holds Site. And we run the times iterator 1 time  
and invoke Factory() which defines a series of attributes for a Site  
object, for example:

Factory.define :site do |f|  
f.sequence(:number) { |n| “TEST-#{n}” }  
f.state { SiteState[:initialized] }  
f.target 50  
f.est Date.current  
end

So now we have object and some attributes stored in a block that gets  
passed to group class method. The &block indicates that the block is  
passed by reference. We use the yield keyword to yield the contents of  
the block in a hash of the transaction() method. But exactly does it do?

The ruby documentation just says it protects sql statements by rolling  
back if transaction fails:

> **[ActiveRecord::Transactions::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html)**
>
> Active Record Transactions Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action.

But that’s not helpful in this case, as to what transaction() is doing  
above.

Thanks for response.

I no longer receive emails from the board when questions are asked and  
answered. I hope I am still on the board.
