[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting Access to a Row just Created

KevinSimonson

12/8/2007 6:40:00 PM

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_
1 Answer

Rob Biedenharn

12/8/2007 10:35:00 PM

0


On Dec 8, 2007, at 1:40 PM, kvnsmnsn@hotmail.com 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...
>
>
> 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://agileconsult...
Rob@AgileConsultingLLC.com