# Get the id of the last record from a table

**URL:** <https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134>\
**Category:** Rails\
**Created:** [October 30, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134 "2008-10-30T13:06:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ishara\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@Ishara\_G](https://www.ruby-forum.com/u/Ishara_G)\
**Post date:** [October 30, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134/1 "2008-10-30T13:06:32Z")

</div>

can anyone tell me how can i get the last id(primary key) of the table

---

<div class="post-metadata">

**Author:** ![Ishara\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@Ishara\_G](https://www.ruby-forum.com/u/Ishara_G)\
**Post date:** [October 30, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134/2 "2008-10-30T13:09:31Z")

</div>

i don’t know if i understand correctly what you want to do, but you  
could just type:  
YourObjectName.last

On 30 Okt., 13:05, “Ishara G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Ishara\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@Ishara\_G](https://www.ruby-forum.com/u/Ishara_G)\
**Post date:** [October 30, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134/3 "2008-10-30T13:11:17Z")

</div>

oh forgot to ask for id:  
YourObjectName.last.id

---

<div class="post-metadata">

**Author:** ![Ishara\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@Ishara\_G](https://www.ruby-forum.com/u/Ishara_G)\
**Post date:** [October 30, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134/4 "2008-10-30T13:14:08Z")

</div>

If you want only the id of the last entered data then try this,  
User.maximum(‘id’)  
This will return the maximum id which will be anyways the last one.

Ishara G. wrote:

> can anyone tell me how can i get the last id(primary key) of the table

---

<div class="post-metadata">

**Author:** ![Ishara\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e68b1a/32.png) [@Ishara\_G](https://www.ruby-forum.com/u/Ishara_G)\
**Post date:** [October 30, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/get-the-id-of-the-last-record-from-a-table/152134/5 "2008-10-30T13:28:55Z")

</div>

thanx,i did it,it was success

2008/10/30 Naga harish Kanegolla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
