# How to make unique ids for URLs

**URL:** <https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022>\
**Category:** Rails\
**Created:** [August 28, 2010, 6:07am UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022 "2010-08-28T06:07:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 6:07am UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/1 "2010-08-28T06:07:02Z")

</div>

The path I currently have for a record in the post table is this:

[www.myrailsapp.com/post/id](http://www.myrailsapp.com/post/id)

here’s the route:

map.resources :posts, :has\_many =\> :comments

Any thoughts on how you would make the above this?

[www.myrailsapp.com/1djs34](http://www.myrailsapp.com/1djs34) (or some join of randomly generated letters  
and numbers) so that users easily bookmark the page and access it later.

I’d want to replace the traditional path id with something more random.  
How would I make an extra column in the post table to do this?

Did some google searching, but I guess I’m not hitting the right  
keywords.

Thanks!

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 10:01am UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/2 "2010-08-28T10:01:42Z")

</div>

On 28 August 2010 05:07, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [www.myrailsapp.com/1djs34](http://www.myrailsapp.com/1djs34) (or some join of randomly generated letters  
> and numbers) so that users easily bookmark the page and access it later.

Why cannot they bookmark page [www.myrailsapp.com/post/631](http://www.myrailsapp.com/post/631) just as  
easily as www…/1djs32?

Colin

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 2:14pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/3 "2010-08-28T14:14:43Z")

</div>

I suppose they could, but I’d like to make the URL path as short as  
possible. Plus, it’s a excuse to learn how it works :).

Colin L. wrote:

> On 28 August 2010 05:07, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > [www.myrailsapp.com/1djs34](http://www.myrailsapp.com/1djs34) (or some join of randomly generated letters  
> > and numbers) so that users easily bookmark the page and access it later.
> 
> Why cannot they bookmark page [www.myrailsapp.com/post/631](http://www.myrailsapp.com/post/631) just as  
> easily as www…/1djs32?
> 
> Colin

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/4 "2010-08-28T16:24:29Z")

</div>

On Sat, Aug 28, 2010 at 5:14 AM, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I suppose they could, but I’d like to make the URL path as short as  
> possible.

Why? What possible value does an obfuscated and shortened URL  
offer anyone?

If anything, most users would get more value out of something like

[www.myrailsapp.com/post/631-what-were-they-thinking](http://www.myrailsapp.com/post/631-what-were-they-thinking)

which at least gives the user (as well as search engines!) _some_  
meta-information about the content of the resource…

> Plus, it’s a excuse to learn how it works :).

…and you could google `rails friendly urls` for implementation examples  
to study 🙂

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/5 "2010-08-28T17:03:43Z")

</div>

On Sat, Aug 28, 2010 at 7:30 AM, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That is true… I guess what I’m trying to get at is avoid using the  
> built in ids as the resource identifier… and instead just using a  
> randomly generated number as the identifier:

Again, I don’t know why you’d bother 🙂 but you could generate a  
separate number on creation and use that – google ‘rails UUID’ for  
one approach.

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/6 "2010-08-28T17:09:28Z")

</div>

Awesome, thanks Hassan for pointing me in the right direction.

Hassan S. wrote:

> On Sat, Aug 28, 2010 at 7:30 AM, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > That is true… I guess what I’m trying to get at is avoid using the  
> > built in ids as the resource identifier… and instead just using a  
> > randomly generated number as the identifier:
> 
> Again, I don’t know why you’d bother 🙂 but you could generate a  
> separate number on creation and use that – google ‘rails UUID’ for  
> one approach.
> 
> ## HTH,
> 
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/7 "2010-08-28T16:30:54Z")

</div>

That is true… I guess what I’m trying to get at is avoid using the  
built in ids as the resource identifier… and instead just using a  
randomly generated number as the identifier:

[www.myrailsapp.com/post/what-were-they-thinking-184952](http://www.myrailsapp.com/post/what-were-they-thinking-184952)

Hassan S. wrote:

> On Sat, Aug 28, 2010 at 5:14 AM, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I suppose they could, but I’d like to make the URL path as short as  
> > possible.
> 
> Why? What possible value does an obfuscated and shortened URL  
> offer anyone?
> 
> If anything, most users would get more value out of something like
> 
> [www.myrailsapp.com/post/631-what-were-they-thinking](http://www.myrailsapp.com/post/631-what-were-they-thinking)
> 
> which at least gives the user (as well as search engines!) _some_  
> meta-information about the content of the resource…
> 
> > Plus, it’s a excuse to learn how it works :).
> 
> …and you could google `rails friendly urls` for implementation examples  
> to study 🙂
> 
> ## FWIW,
> 
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/8 "2010-08-28T18:21:00Z")

</div>

Hassan S. wrote:

> On Sat, Aug 28, 2010 at 5:14 AM, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I suppose they could, but I’d like to make the URL path as short as  
> > possible.
> 
> Why? What possible value does an obfuscated and shortened URL  
> offer anyone?

It’s more Twitter-friendly. See  
[http://groups.google.com/group/techvalleyrb/msg/ac3ce4ae6620268a?pli=1](http://groups.google.com/group/techvalleyrb/msg/ac3ce4ae6620268a?pli=1)  
for one possibility.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [August 28, 2010, 6:52pm UTC](https://www.ruby-forum.com/t/how-to-make-unique-ids-for-urls/194022/9 "2010-08-28T18:52:35Z")

</div>

On Sat, Aug 28, 2010 at 9:21 AM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Why? What possible value does an obfuscated and shortened URL  
> > offer anyone?
> 
> It’s more Twitter-friendly.

Point taken. But I’d still rather see a human/SEO-friendly URL in my  
browser and as appropriate provide a “tweetable” version on the page  
🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan
