# Getting Access to a Row just Created

**URL:** <https://www.ruby-forum.com/t/getting-access-to-a-row-just-created/122524>\
**Category:** Ruby\
**Created:** [December 8, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/getting-access-to-a-row-just-created/122524 "2007-12-08T19:41:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 8, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/getting-access-to-a-row-just-created/122524/1 "2007-12-08T19:41:01Z")

</div>

I’ve got a Ruby file where I take a table from my database named “Poc”  
and I’ve created a row in that table by executing:

```
 Poc.create()

```

How do I access the id of the row I’ve just created? I could do some-  
thing like this:

```
 Poc.create( :user_name => '')

```

and then do a “Poc.find()” that searches for a row with the empty  
string as its “user\_name”, but seems like such a round-about way of  
doing it. Is there a more direct way?

```
                            ---Kevin Simonson

```

“You’ll never get to heaven, or even to LA,  
if you don’t believe there’s a way.”  
from _Why Not_

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 8, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/getting-access-to-a-row-just-created/122524/2 "2007-12-08T23:36:17Z")

</div>

On Dec 8, 2007, at 1:40 PM, removed\_email\_address@domain.invalid wrote:

> I’ve got a Ruby file where I take a table from my database named “Poc”  
> and I’ve created a row in that table by executing:
> 
> ```
> Poc.create()
> 
> ```

new\_one = Poc.create()

Then do what you want with new\_one.id or any other field. However…

> ```
> ---Kevin Simonson
> 
> ```
> 
> “You’ll never get to heaven, or even to LA,  
> if you don’t believe there’s a way.”  
> from _Why Not_

…you ususally either do Poc.new or Poc.create with a hash of  
attributes.

Then, if you started with Poc.new, you .save the record when you’re  
ready.  
Poc.create automatically saves, but if you change the record after you  
create it, you’d have to .save that record, too.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
