Getting Access to a Row just Created

K

kvnsmnsn

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_
 
R

Rob Biedenharn

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...
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_


...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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top