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


#1

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!


#2

Don’t you still have @post?

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


#3

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


#4

Try: puts @post.inspect

and check your logs

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


#5

It works!

I’ve tried @post.id !