# Get ID after save

**URL:** <https://www.ruby-forum.com/t/get-id-after-save/54433>\
**Category:** Rails\
**Created:** [March 4, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433 "2006-03-04T21:19:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/1 "2006-03-04T21:19:21Z")

</div>

This is simple (I am sure) but how do you get the id of the inserted  
element after the element has been save?

I thought it was something like this:

@product = Product.new(params[:product]

if @product.save  
add\_image(product.id, params[“image”])  
flash[:notice] = ‘Product was successfully created.’  
redirect\_to :action =\> ‘list\_products’  
end

But that isn’t it

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/2 "2006-03-04T21:32:18Z")

</div>

i think the reference to product in add\_image(product\_id… is missing  
‘@’  
to reference the instance variable, not the a local variable.

so it would be add\_image(@product.id, params[:product])

I could be wrong, but try it out.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/3 "2006-03-04T21:32:19Z")

</div>

You should be able to reference product as “@product.id” instead of  
“product.id”.

If that doesn’t work, do “@product.reload”, and you should then have the  
“id”.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/4 "2006-03-04T21:53:08Z")

</div>

I get a nil error when I do this. Even after the @product.reload.

Michael G. wrote:

> You should be able to reference product as “@product.id” instead of  
> “product.id”.
> 
> If that doesn’t work, do “@product.reload”, and you should then have the  
> “id”.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/5 "2006-03-04T21:56:31Z")

</div>

And you’ve confirmed that you’re calling “@product.id” and not  
“product.id”?

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/6 "2006-03-04T21:59:32Z")

</div>

And… are you sure that params[“image”] isn’t nil?

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/7 "2006-03-04T22:01:26Z")

</div>

Michael G. wrote:

> And… are you sure that params[“image”] isn’t nil?

Sorry. You were right.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [March 4, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/get-id-after-save/54433/8 "2006-03-04T22:05:34Z")

</div>

Not a problem at all - I’ve run into this before.
