Forum: Ruby on Rails How to get the ID of the object I just saved in the DB?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-23 01:25
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!
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-23 02:14
(Received via mailing list)
Don't you still have @post?

On Feb 22, 4:25 pm, Guillaume Loader <rails-mailing-l...@andreas-
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-23 02:24
What do you mean?
That I should do this : @post.post_id ?
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-23 02:33
(Received via mailing list)
Try: puts @post.inspect

and check your logs

On Feb 22, 5:24 pm, Guillaume Loader <rails-mailing-l...@andreas-
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-23 03:01
It works!

I've tried @post.id !
This topic is locked and can not be replied to.