# Find current Id

**URL:** <https://www.ruby-forum.com/t/find-current-id/125694>\
**Category:** Rails\
**Created:** [January 11, 2008, 7:32am UTC](https://www.ruby-forum.com/t/find-current-id/125694 "2008-01-11T07:32:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 11, 2008, 7:32am UTC](https://www.ruby-forum.com/t/find-current-id/125694/1 "2008-01-11T07:32:44Z")

</div>

Hi,  
Can someine tell me how to find the latest/current id from the database?

Thanks,  
fries 88

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 11, 2008, 7:48am UTC](https://www.ruby-forum.com/t/find-current-id/125694/2 "2008-01-11T07:48:29Z")

</div>

I thought you already got this answered?

Model.find(:first, :order =\> “id DESC”).id

On Jan 11, 2008 5:02 PM, fries 88 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 11, 2008, 10:31am UTC](https://www.ruby-forum.com/t/find-current-id/125694/3 "2008-01-11T10:31:19Z")

</div>

On 11 Jan 2008, at 07:47, fredistic wrote:

> ‘whatever.object\_id’. The id that save gives you is right for your  
> new object, but, because of the race condition, is not guaranteed to  
> be the latest and greatest in the table.

The whole concept of ‘greatest’/‘latest’ id is subject to race  
conditions. Why do you need to the latest id in the db ?  
Fred

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 11, 2008, 8:48am UTC](https://www.ruby-forum.com/t/find-current-id/125694/4 "2008-01-11T08:48:43Z")

</div>

This is subject to race conditions. If you get the latest id, and  
then use it, it is possible that by the time you use it, some other  
user has inserted a new row and the highest id is now different. This  
is an inherent property of any good RDBMS.

If your controller does a ‘modelrecord.save’ call, and it succeeds,  
the id is immediately stored in the record as ‘modelrecord.id’, but I  
prefer the form ‘modelrecord[:id]’, as the first form is confusable  
with the deprecated Ruby method that is now available as  
‘whatever.object\_id’. The id that save gives you is right for your  
new object, but, because of the race condition, is not guaranteed to  
be the latest and greatest in the table.

F
