# Knowing id before a call to save

**URL:** <https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819>\
**Category:** Rails\
**Created:** [May 17, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819 "2006-05-17T19:41:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 17, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819/1 "2006-05-17T19:41:04Z")

</div>

Hiall,

Is it possible to find out the value for the id field of any record  
before it was actually saved, i.e. if new\_record? returns true?

To motivate this:  
I have a parent model which should not exists alone but needs a few  
other (child) models to be initialized whenever a new record of parent  
is instantiated. Needless to say, the child models need the id of the  
parent model as foreign key. I know I could do that in a call to a  
public parent model method after I saved it, but I want this to be  
done in the initialize method. Yes, I have setup my has\_one and  
has\_many relations.

Any ideas?

cheers  
Martin

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 17, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819/2 "2006-05-17T19:45:16Z")

</div>

The id is assigned by the database not the model, so no. Use an  
observer like after\_update to do the work.

Michael

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 17, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819/3 "2006-05-17T20:25:59Z")

</div>

Hi Michael,

Thx for the tip! I could have thought of the fact the db assigns the  
pk value if following the rails conventions. However, what would it be  
like if the pk wasn’t set to auto\_increment ? This is just out of  
curiosity actually 🙂 I’m a nice guy and follow the rails conventions  
so your solution is perfectly acceptable for me, although I might go  
with a simple callback method not a full blown observer 🙂

Thx!  
Martin

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 17, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819/4 "2006-05-17T20:37:39Z")

</div>

If not following convention, and you’re assigning the pk, well you’re  
assigning the pk. In that case you have it ahead of time and you can  
create the children first. The only gotcha is if you have foreign key  
constraints in your database, that may prevent you from doing so.

I would also not use an observer in that case.

Michael

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 17, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/knowing-id-before-a-call-to-save/60819/5 "2006-05-17T20:56:45Z")

</div>

Hehe sillly me! Now that you say it, once again it’s so obvious 🙂 It  
seems I am a little bit slow thinking today 🙂 Thx anyway for your  
fast an precise response!

cheers  
Martin
