# ADVICE: Generating unique identifiers for later DB insertion

**URL:** <https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197>\
**Category:** Rails\
**Created:** [June 23, 2006, 12:03am UTC](https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197 "2006-06-23T00:03:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 23, 2006, 12:03am UTC](https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197/1 "2006-06-23T00:03:12Z")

</div>

All,

I have an ActiveRecord object that I need to be able to uniquely  
identify _before_ I actually save the object (I may not end up saving  
the object based on conditions). This is because I want to use the ID  
in another context (a file name in the filesystem).

Depending on conditions, I may or may not need to do a lot of stuff  
between the time that I generate the object ID and actually make the  
decision to save the object. I am toying with the idea of using an ID  
generator for this object.

I wanted to get some input on using an “external to the DB” generator to  
create my unique ids for insertion into the DB.

Am I overthinking this? Should I just do the insert as soon as possible  
to have the DB generate my ID, use the ID as I need to, and then either  
delete that record (or rollback a transaction) later if it turns out  
that the record shouldn’t be saved after all?

Hope this makes sense.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 23, 2006, 12:12am UTC](https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197/2 "2006-06-23T00:12:49Z")

</div>

On 6/22/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am I overthinking this? Should I just do the insert as soon as possible  
> to have the DB generate my ID, use the ID as I need to, and then either  
> delete that record (or rollback a transaction) later if it turns out  
> that the record shouldn’t be saved after all?

Yes, and no. I generally recommend not tying anything like this to  
the primary key of a record. It’s much better to create an extra  
column (VARCHAR of appropriate size) and use that as the file key. It  
will still be saved with the record and you can still let Rails do  
what it wants and maintain full control of the “external” stuff (file  
names, etc).

I would recommend maybe a randomized MD5 hash system or such (similar  
to the algorighm Rails uses to make sessions). UserID (or something)

- Now (DateTime) + Random Number run through the MD5 hashing method to  
generate a string you can use for the file name and data column. Your  
ActiveRecord model subclass will happily work with the data and save /  
retrieve it from the database. It also gives you an almost gauranteed  
unique value (you can always run a search of the database and  
regenerate if it’s not unique–if you’re paranoid)

-Curtis

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 23, 2006, 1:17am UTC](https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197/3 "2006-06-23T01:17:00Z")

</div>

Curtis S. wrote:

> On 6/22/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Am I overthinking this? Should I just do the insert as soon as possible  
> > to have the DB generate my ID, use the ID as I need to, and then either  
> > delete that record (or rollback a transaction) later if it turns out  
> > that the record shouldn’t be saved after all?
> 
> Yes, and no. I generally recommend not tying anything like this to  
> the primary key of a record. It’s much better to create an extra  
> column (VARCHAR of appropriate size) and use that as the file key. It  
> will still be saved with the record and you can still let Rails do  
> what it wants and maintain full control of the “external” stuff (file  
> names, etc).
> 
> I would recommend maybe a randomized MD5 hash system or such (similar  
> to the algorighm Rails uses to make sessions). UserID (or something)
> 
> - Now (DateTime) + Random Number run through the MD5 hashing method to  
> generate a string you can use for the file name and data column. Your  
> ActiveRecord model subclass will happily work with the data and save /  
> retrieve it from the database. It also gives you an almost gauranteed  
> unique value (you can always run a search of the database and  
> regenerate if it’s not unique–if you’re paranoid)
> 
> -Curtis

Curtis,

I agree. Thanks for the advice. I ended up using user\_id +  
Time.now.to\_f.to\_s (with the fractional seconds I’m pretty confident  
that the identifier will be unique).

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 23, 2006, 6:50am UTC](https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197/4 "2006-06-23T06:50:57Z")

</div>

On Jun 22, 2006, at 6:17 PM, Wes G. wrote:

> > the primary key of a record. It’s much better to create an extra  
> > generate a string you can use for the file name and data column.
> 
> Curtis,
> 
> I agree. Thanks for the advice. I ended up using user\_id +  
> Time.now.to\_f.to\_s (with the fractional seconds I’m pretty confident  
> that the identifier will be unique).

Take a look at the “uuid” gem. It will generate unique numbers on a  
system and is already packaged up as a gem.

cr

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 23, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/advice-generating-unique-identifiers-for-later-db-insertion/64197/5 "2006-06-23T13:24:54Z")

</div>

On Thursday, June 22, 2006, at 11:48 PM, wrote:

> > > > that the record shouldn’t be saved after all?  
> > > > to the algorighm Rails uses to make sessions). UserID (or something)
> 
> system and is already packaged up as a gem.
> 
> cr
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

MySQL also has a UUID() function, but this approach may not be database  
agnostic.

\_Kevin
