# How to capture the last DB insertion id?

**URL:** <https://www.ruby-forum.com/t/how-to-capture-the-last-db-insertion-id/73323>\
**Category:** Rails\
**Created:** [February 26, 2006, 1:28am UTC](https://www.ruby-forum.com/t/how-to-capture-the-last-db-insertion-id/73323 "2006-02-26T01:28:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mats\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@Mats\_P](https://www.ruby-forum.com/u/Mats_P)\
**Post date:** [February 26, 2006, 1:28am UTC](https://www.ruby-forum.com/t/how-to-capture-the-last-db-insertion-id/73323/1 "2006-02-26T01:28:33Z")

</div>

Hi all,

I’ve got a model - Images - and when I insert a new image into that  
db,table I would like to grab hold of the last insertion id of that  
row/record and then process it further and save reworked version back  
into the db.table again.

Example: a new image gets inserted as a db.table.row with ID =  
12345678 (id = primary key)  
The ID (12345678) is then retrieved and processed and inserted as  
‘human\_ref’ where it’s styled as 1234-5678 in the same db.table.row.

Any ideas of how to do this in Rails ? I guess I can use the  
‘after\_save’ callback inside the model - Image - to do the  
processing, but how could I get hold of the last insertion id  
easily ? IF AR is currently not able to present this last insertion  
id easily, could a ‘AR.last\_insertion\_id’ method be created and added  
to Ralls ?

Else I guess I would have to load the Image object again with a  
select sql and then retrieve the id, but that seem expensive and a  
bit complicated. I plan to insert big batches of images in this way,  
ie: 100+ images in one go, so not having to execute a bundle of  
queries repeatedly would be great from many different angles.

Any advice/guidance would be very much appreciated.

Kind regards,

Mats

* * *

“TextMate, coding with an incredible sense of joy and ease”

- [www.macromates.com](http://www.macromates.com) -

---

<div class="post-metadata">

**Author:** ![Mats\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@Mats\_P](https://www.ruby-forum.com/u/Mats_P)\
**Post date:** [February 26, 2006, 1:53am UTC](https://www.ruby-forum.com/t/how-to-capture-the-last-db-insertion-id/73323/2 "2006-02-26T01:53:21Z")

</div>

I am not sure if this is correct or not but i belive if you do  
Image.save the id will be automatically set so you can just use Image.id  
from then on. Though don’t quote me as i am still new to Rails also.

---

<div class="post-metadata">

**Author:** ![Mats\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@Mats\_P](https://www.ruby-forum.com/u/Mats_P)\
**Post date:** [February 27, 2006, 12:19am UTC](https://www.ruby-forum.com/t/how-to-capture-the-last-db-insertion-id/73323/3 "2006-02-27T00:19:16Z")

</div>

On 26 Feb 2006, at 01:53, Rob S. wrote:

> I am not sure if this is correct or not but i belive if you do  
> Image.save the id will be automatically set so you can just use  
> Image.id from then on. Though don’t quote me as i am still new to  
> Rails also.

Thanks Rob!

Dang, I do love Ruby & Rails! This is all the code required to do  
what I wanted to achieve. Sometimes it’s just too easy 😉  
Although I’m sure that someone else can improve on this code even  
further.

class Image \< ActiveRecord::Base

# various validation routines and other bits

## After Create method

after\_create :create\_img\_ref

private

def create\_img\_ref  
the\_id = self.id.to\_s  
# split the 8 digit id into XXXX-XXXX format with a dash  
self.img\_ref = “#{the\_id[0,4]}-#{the\_id[4,8]}”  
# then save it to the db record via an update  
self.update  
end

end

Hope this may help someone else someday.

Kind regards,

Mats

* * *

“TextMate, coding with an incredible sense of joy and ease”

- [www.macromates.com](http://www.macromates.com) -
