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
> 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
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 -- Primalgrasp LLC http://primalgrasp.com
On 2/21/06, G?bor Sebesty?n <segabor@gmail.com> wrote:> > 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? >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.
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