# =?iso-8859-2?q?rekord\_id-je\_=2Ecreate\_ut=E1n?=

**URL:** <https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045>\
**Category:** Rails\
**Created:** [February 21, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045 "2006-02-21T16:23:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GSSSSbor\_SebestySSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@GSSSSbor\_SebestySSS](https://www.ruby-forum.com/u/GSSSSbor_SebestySSS)\
**Post date:** [February 21, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045/1 "2006-02-21T16:23:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GSSSSbor\_SebestySSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@GSSSSbor\_SebestySSS](https://www.ruby-forum.com/u/GSSSSbor_SebestySSS)\
**Post date:** [February 21, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045/2 "2006-02-21T18:26:43Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![GSSSSbor\_SebestySSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@GSSSSbor\_SebestySSS](https://www.ruby-forum.com/u/GSSSSbor_SebestySSS)\
**Post date:** [February 21, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045/3 "2006-02-21T19:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GSSSSbor\_SebestySSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@GSSSSbor\_SebestySSS](https://www.ruby-forum.com/u/GSSSSbor_SebestySSS)\
**Post date:** [February 21, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045/4 "2006-02-21T23:37:39Z")

</div>

On 2/21/06, GÃ¡bor SebestyÃ©n [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![GSSSSbor\_SebestySSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@GSSSSbor\_SebestySSS](https://www.ruby-forum.com/u/GSSSSbor_SebestySSS)\
**Post date:** [February 22, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/iso-8859-2-q-rekord-id-je-2ecreate-ut-e1n/54045/5 "2006-02-22T14:45:49Z")

</div>

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
