Forum: Ruby on Rails how do i retrieve a newly created id

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-03-11 22:48
(Received via mailing list)
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
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-03-11 23:09
(Received via mailing list)
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 White <ljw1001@gmail.com> wrote:
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>


--
Ben Reubenstein
http://www.benr75.com
91a469d9f43a066babfd5531311d0fd1?d=identicon&s=25 Rath --- (rath)
on 2006-03-12 01:02
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 Reubenstein 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 White <ljw1001@gmail.com> wrote:
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>
>
> --
> Ben Reubenstein
> http://www.benr75.com
This topic is locked and can not be replied to.