# How to find out autoincrement id?

**URL:** <https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422>\
**Category:** Rails\
**Created:** [May 22, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422 "2008-05-22T21:33:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [May 22, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422/1 "2008-05-22T21:33:13Z")

</div>

Hi

I need to push newly created item id to file. (ie. its autoincrement  
value)

I tried this on controller but it wont work, it cant find id value for  
the newly created item.

def create  
@imitem = Imitem.new(params[:imitem])

system “echo “@imitem.id” \> /tmp/myid”

any help ?

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [May 22, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422/2 "2008-05-22T21:41:29Z")

</div>

You have to save the record to the database to get an id, so you must  
use #create instead of #new or call #save after you create the object.

–Jeremy

On Thu, May 22, 2008 at 3:32 PM, KTU [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @imitem = Imitem.new(params[:imitem])
> 
> system “echo "@imitem.id" \> /tmp/myid”
> 
> any help ?
> 
> >

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com](http://entp.com)

Read my books:  
Ruby in Practice ([Ruby in Practice](http://manning.com/mcanally/))  
My free Ruby e-book ([http://humblelittlerubybook.com/](http://humblelittlerubybook.com/))

Or, my blogs:

> **[Site not found · DreamHost](http://mrneighborly.com)**
>
> The owner of this domain has not yet uploaded their website.

[http://rubyinpractice.com](http://rubyinpractice.com)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [May 22, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422/3 "2008-05-22T21:50:57Z")

</div>

Well I have it on create function like this but this just prints  
@imitem.id string to file no the real id number

def create  
@imitem = Imitem.new(params[:imitem])

```
respond_to do |format|

  if @imitem.save
    flash[:notice] = 'Imitem was successfully created.'
  format.html { redirect_to(imitems_url) }
    format.xml { render :xml => @imitem, :status

```

=\> :created, :location =\> @imitem }

system “echo "@imitem.id" \> /tmp/myid”

```
  else
    format.html { render :action => "new" }
    format.xml { render :xml => @imitem.errors, :status

```

=\> :unprocessable\_entity }  
end  
end

On May 22, 10:40 pm, “Jeremy McAnally” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [May 22, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422/4 "2008-05-22T21:55:01Z")

</div>

KTU wrote:

> Well I have it on create function like this but this just prints  
> @imitem.id string to file no the real id number
> 
> system “echo “@imitem.id” \> /tmp/myid”

Well you are just printing a string. Your @imitem.id is not being  
evaluated as ruby code, just raw text.

Try:

system “echo “#{@imitem.id}” \> /tmp/myid”

The #{ … } is a double quoted string evalulates its content as ruby,  
and puts the output in that spot in the string.

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [May 22, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/how-to-find-out-autoincrement-id/138422/5 "2008-05-22T21:52:41Z")

</div>

KTU wrote:

> Hi
> 
> I need to push newly created item id to file. (ie. its autoincrement  
> value)
> 
> I tried this on controller but it wont work, it cant find id value for  
> the newly created item.
> 
> def create  
> @imitem = Imitem.new(params[:imitem])
> 
> system “echo “@imitem.id” \> /tmp/myid”
> 
> any help ?

It won’t assign an ID until you save it. Add a:

@imitem.save

Or change your code to:

@imitem = Imitem.create(params[:imitem])

Then @imitem.id will have a value. All new records have an id of nil.
