How do i retrieve a newly created id


#1

in my code i’m calling save on an object. The id gets created as the
object
is persisted. How can i retrieve that newly created id? I guess I
could
query the table again using the other attributes of the object but is
there
another way?

thanks in advance.

larry


#2

If I am understanding you correctly you have:

myObj = Object.new()

myObj.save

You can now just use:

myObj.id now that it has been saved.

~ Ben

On 3/11/06, Larry W. removed_email_address@domain.invalid wrote:

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Ben R.
http://www.benr75.com


#3

Hi Ben

Suppose you have an object (@myObj) and another object wich is a
subclass of the first one (using has_one and belongs_to looks like
@myObj.subObj) If I want to save them to their (different) databases: Do
I have to save first the @subObj and then the @myObj or is there an
easier way to save objects into their specific database?

I mean, if I have
@myObj = Object.new()
@subObj = SubObject.new(…)

Can I do something like this?
@myObj.subObj = @subObj
@myObj.save

Or do I have to do something like this?
@subObj.save
@myObj.subObj = @subObj
@myObj.save

Thanks,

Rafa

Ben R. wrote:

If I am understanding you correctly you have:

myObj = Object.new()

myObj.save

You can now just use:

myObj.id now that it has been saved.

~ Ben

On 3/11/06, Larry W. removed_email_address@domain.invalid wrote:

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Ben R.
http://www.benr75.com