Forum: Ruby on Rails =?iso-8859-2?q?rekord_id-je_=2Ecreate_ut=E1n?=

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.
Aa1126cd36ac8570684b7923cff2a6c1?d=identicon&s=25 Gábor Sebestyén (Guest)
on 2006-02-21 16:23
(Received via mailing list)
Hali!

Mi a legegyszerűbb módja annak, hogy egy pl. note = Notes.create(...)
után megkapjam az adott note ActiveRecord id-ját (primary key)? Sajnos
ez ugye 0, mivel frissen lett létrehozva...
Ã?tlet?

Gábor
Aa1126cd36ac8570684b7923cff2a6c1?d=identicon&s=25 Gábor Sebestyén (Guest)
on 2006-02-21 18:26
(Received via mailing list)
> Hali!
>
> Mi a legegyszerűbb módja annak, hogy egy pl. note = Notes.create(...)
> után megkapjam az adott note ActiveRecord id-ját (primary key)? Sajnos
> ez ugye 0, mivel frissen lett létrehozva...
Sorry, missed the proper mailing list :)

So how can I determine a newly created record's primary key (id) after
issuing create method? Consider I have the following: note =
Notes.create(...). It will create a Notes record but note.id will be
zero instead of having valid primary key.
I know this is normal in term of RoR but is there an easy way to
retrieve the id of this object? Do I have to find the record again to
have its primary key?

Thanks,

Gábor
424f838c3f6d203fa5e7298ba7d0af59?d=identicon&s=25 Eszter Biro (eszter)
on 2006-02-21 19:55
(Received via mailing list)
Gábor Sebestyén wrote:
> So how can I determine a newly created record's primary key (id) after
> issuing create method? Consider I have the following: note =
> Notes.create(...).

ActiveRecord's create method instantly saves the record, the object
returned should have a valid id.
Perhaps the validation fails that's why you are getting the unsaved
object with the id value nil.

Eszter
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-02-21 23:37
(Received via mailing list)
On 2/21/06, Gábor Sebestyén <segabor@gmail.com> wrote:
> zero instead of having valid primary key.
> I know this is normal in term of RoR but is there an easy way to
> retrieve the id of this object? Do I have to find the record again to
> have its primary key?
>

As Biro said, Note.create should include the correct id.
Make sure you're passing enough data to pass the validations, though.
To make sure, try Note.create! instead of Note.create.  That will
throw an Exception if the created object couldn't be saved.
Aa1126cd36ac8570684b7923cff2a6c1?d=identicon&s=25 Gábor Sebestyén (Guest)
on 2006-02-22 14:45
(Received via mailing list)
No luck. Object saves but it's id remain zero (0). I can see the
created record in the database with valid primary key (pk = 24,
note.id = 0).
I use the latest rails distribution with sqlite3 for testing purposes.

Any hint?

Gábor
This topic is locked and can not be replied to.