# Can i get the id of a new object before saving it/as i save?

**URL:** <https://www.ruby-forum.com/t/can-i-get-the-id-of-a-new-object-before-saving-it-as-i-save/127037>\
**Category:** Rails\
**Created:** [January 24, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/can-i-get-the-id-of-a-new-object-before-saving-it-as-i-save/127037 "2008-01-24T13:30:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 24, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/can-i-get-the-id-of-a-new-object-before-saving-it-as-i-save/127037/1 "2008-01-24T13:30:22Z")

</div>

I have a batch-add page for users that needs to auto-generate a unique  
login name for each user. What i want to do is set their login name to  
be “#{first\_name}\_#{id}”. The problem is - i can’t see the id until i  
save the object, but i don’t want to save it until i’ve set the login.

Is there any way around this? Can i start creating an object and tell  
it to use the id that will created when it’s saved? Or do i need to  
save it, get the id and then save it again?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 24, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/can-i-get-the-id-of-a-new-object-before-saving-it-as-i-save/127037/2 "2008-01-24T13:57:41Z")

</div>

On 24/01/2008, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a batch-add page for users that needs to auto-generate a unique  
> login name for each user. What i want to do is set their login name to  
> be “#{first\_name}\_#{id}”. The problem is - i can’t see the id until i  
> save the object, but i don’t want to save it until i’ve set the login.

There is no id until you save it to the database. Rails (and any other  
application) cannot tell what the next available id in the database will  
be.  
You could try to determine the highest used id in the database table and  
increment this id but this does not guarantee that this id id is not  
used  
by another user in the meantime and is no more available.

-Thomas

–  
Thomas P.  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid  
BÃ¼ro: 030 - 830 353 88  
mobil: 0176 - 75 03 03 04  
Privat: 030 - 49 78 37 06

> **[thopre](https://thopre.wordpress.com/)**
>
> Entwicklungen für die Bühne des Lebens

[http://www.thopre.com/](http://www.thopre.com/)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 24, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/can-i-get-the-id-of-a-new-object-before-saving-it-as-i-save/127037/3 "2008-01-24T14:05:20Z")

</div>

Hi,  
There is one filter called “after\_save”, it will invoke a method  
(let’s say a foo method) after model.save! succeed (here, you already  
know the id). I figure you can use the foo method to update the login  
name"#{first\_name}\_#{id}" column in database.  
However, I still take this is somehow wired… it updates database  
twice… unless you really have the reason to do this.

P.s. I dont think there is a good way to know the id beforehand unless  
you really stored this user, cuz even you are able to know the last id  
in database, you still cant guarantee that there is nobody  
registering just one second before you!

Br,  
MyST

On Jan 24, 2:30 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 24, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/can-i-get-the-id-of-a-new-object-before-saving-it-as-i-save/127037/4 "2008-01-24T14:30:37Z")

</div>

myst\_tt wrote:

> Hi,  
> There is one filter called “after\_save”, it will invoke a method  
> (let’s say a foo method) after model.save! succeed (here, you already  
> know the id). I figure you can use the foo method to update the login  
> name"#{first\_name}\_#{id}" column in database.

Thanks myst\_tt, i don’t want to use a callback because the situation’s a  
little more complicated than i described: by default, user’s logins are  
set the same as their email address, but emails aren’t forced to be  
unique (since a group of users can share an email). So, i actually only  
need to autogenerate a login some of the time (a minority of cases  
actually).

What i’ve ended up doing (and it seems dirty) is make a temporary  
probably-unique login by combining first name and time since epoch in  
microseconds - rails/mysql is unlikely to ever manage 2 transactions in  
one microsecond so this should be fine. Then, after saving, i call  
update\_attributes to set it to firstname+id, which is guaranteed to be  
unique and also more memorable than firstname+microseconds\_since\_epoch.

Like i say, i’m not very happy with this though.
