# Getting auto-generated id without calling save

**URL:** <https://www.ruby-forum.com/t/getting-auto-generated-id-without-calling-save/91689>\
**Category:** Rails\
**Created:** [April 11, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/getting-auto-generated-id-without-calling-save/91689 "2007-04-11T16:42:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [April 11, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/getting-auto-generated-id-without-calling-save/91689/1 "2007-04-11T16:42:08Z")

</div>

Hi,

I have a bunch of models that are all related to each other through  
various has\_one, has\_many, belongs\_to relationships. I am trying to  
create a set of these related objects when parsing through an XML  
file, but would like to save the set as a whole through the use of  
transactions, to cut down on the number of SQL COMMITs that are  
called, as these are slowing down my app by a factor of 2 to 3  
(compared to using transactions). However, as object.save is not  
being called until the transaction is processed, the foreign key  
constraints for the relationships are failing. At first I was getting  
the highest id value from the tables and using these to set the  
values. The problem now is that I am using BackgrounDRb to do the  
processing, and if several XML files come in in sequence, then two or  
more of the BackgrounDRb workers are pulling the same id value from  
the tables, as the transactions have not completed before the next  
worker is generated.

Is there a way to get or set this auto-generated id value without  
saving the object, so that I can continue using transactions without  
causing this race condition?

Thanks,

Simon

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [April 11, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/getting-auto-generated-id-without-calling-save/91689/2 "2007-04-11T19:26:16Z")

</div>

Hi Simon,

You can wrap a set of rails commands in a single transaction by doing…

ActiveRecord::Base.transaction do  
  
end

And it will only do one commit at the end.

Hope this helps,

Dave.
