# How to get the ID of the object I just saved in the DB?

**URL:** <https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055>\
**Category:** Rails\
**Created:** [February 23, 2009, 1:25am UTC](https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055 "2009-02-23T01:25:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 1:25am UTC](https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055/1 "2009-02-23T01:25:28Z")

</div>

Hello everyone!

Does the method save return the ID ?  
Because i’m doing this :

if @post.save  
flash[:notice] = ‘Post was successfully created.’  
end

and I need the ID of the post I just created. I think it could work with  
: Post.find(:last) but what if two people are posting at the same time?

Thank you!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 2:14am UTC](https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055/2 "2009-02-23T02:14:00Z")

</div>

Don’t you still have @post?

On Feb 22, 4:25 pm, Guillaume L. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 2:24am UTC](https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055/3 "2009-02-23T02:24:31Z")

</div>

What do you mean?  
That I should do this : @post.post\_id ?

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 2:33am UTC](https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055/4 "2009-02-23T02:33:18Z")

</div>

Try: puts @post.inspect

and check your logs

On Feb 22, 5:24 pm, Guillaume L. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 23, 2009, 3:01am UTC](https://www.ruby-forum.com/t/how-to-get-the-id-of-the-object-i-just-saved-in-the-db/161055/5 "2009-02-23T03:01:34Z")

</div>

It works!

I’ve tried @post.id !
