Re: [Rails] Re: rekord id-je .create után

Wilson Bilkovich wilsonb at gmail.com
Tue Feb 21 22:34:36 GMT 2006


On 2/21/06, Gábor Sebestyén <segabor at 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.


More information about the Rails mailing list