# How to get the id field before a save

**URL:** <https://www.ruby-forum.com/t/how-to-get-the-id-field-before-a-save/90642>\
**Category:** Rails\
**Created:** [March 30, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-get-the-id-field-before-a-save/90642 "2007-03-30T17:04:49Z")\
**Posts on this page:** 3\
**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:** [March 30, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-get-the-id-field-before-a-save/90642/1 "2007-03-30T17:04:49Z")

</div>

Hi,

I have a situation where I am parsing an XML file, and in the process  
I create several related groups of objects. I am currently creating  
and saving each one in sequence, but this is causing a huge number of  
COMMIT’s to be called in the SQL. What I would like to do, is store  
every object of the same type that is created in an array, and then  
save them all at once, wrapped in a transaction call, so that there is  
only one COMMIT called on every table. The problem I’m having is that  
there is an order of dependency among the objects, so Object2 depends  
on Object1’s id as its foreign key. Without the call to save, Object1  
does not have an id assigned to it, so the eventual save on object 2  
fails. Is there any way to get this value (auto-incrementing in the  
DB) without all the extra overhead?

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:** [March 31, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/how-to-get-the-id-field-before-a-save/90642/2 "2007-03-31T18:07:43Z")

</div>

How would you write SQL to do that? The only way I can think you would  
do it is to use a stored procedure, generate your own primary keys  
(hoping they don’t clash!!!), and do something like an  
IDENTITY\_INSERT. Otherwise you will have to insert the row to get its  
primary key.

If all you need is a single transaction, then maybe you can use  
Object.transaction. But I thought I heard a rumour that that was being  
depreciated. Is that true?

---

<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:** [March 30, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-get-the-id-field-before-a-save/90642/3 "2007-03-30T17:20:51Z")

</div>

On 30 Mar 2007, at 16:03, Simon wrote:

> fails. Is there any way to get this value (auto-incrementing in the  
> DB) without all the extra overhead?

An object doesn’t have to be saved for you to relate other objects to  
it.

Note that the point when objects are saved depends on which way round  
you associate them. So if you assign an object to a has\_one  
relationship in an existing object, that associated object will be  
automatically saved. But if you assign a new object to a belongs\_to  
relationship, it won’t be automatically saved.

See pp 356-357 of Agile Web D. With Rails v2.

Hope that helps.

Regards,  
Andy S.
