# REST with :name as unique identifier, rather than :id

**URL:** <https://www.ruby-forum.com/t/rest-with-name-as-unique-identifier-rather-than-id/91193>\
**Category:** Rails\
**Created:** [April 5, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/rest-with-name-as-unique-identifier-rather-than-id/91193 "2007-04-05T15:01:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 5, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/rest-with-name-as-unique-identifier-rather-than-id/91193/1 "2007-04-05T15:01:40Z")

</div>

Hi all,

Is it possible to implement REST with :name as a unique identifier of  
a model, instead of the :id? Having readable (literally) URLs is  
important to my application as many URLs are announced at conferences  
or read out over the phone.

For example, I’d like to do:

/branches  
/branches/bristol  
/branches/bristol;edit

Cheers,

Tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 5, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/rest-with-name-as-unique-identifier-rather-than-id/91193/2 "2007-04-05T15:36:49Z")

</div>

On Apr 5, 2007, at 9:00 AM, Tom T. wrote:

> /branches/bristol  
> /branches/bristol;edit
> 
> Cheers,
> 
> Tom

In your controller, you just need to replace:

Branch.find(params[:id])

with:

Branch.find\_by\_name(params[:id])

and then possibly add to your Branch model:

class Branch  
def to\_param  
self.name  
end  
end

so URL generation will do the right thing if you have:

edit\_branch\_url(@branch)  
or  
edit\_branch\_url(:id =\> @branch)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [April 5, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/rest-with-name-as-unique-identifier-rather-than-id/91193/3 "2007-04-05T16:28:55Z")

</div>

On 5 Apr, 14:36, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Branch  
> def to\_param  
> self.name  
> end  
> end
> 
> so URL generation will do the right thing if you have:
> 
> edit\_branch\_url(@branch)  
> or  
> edit\_branch\_url(:id =\> @branch)

Thanks Rob,

That’s amazingly simple… Any other gotchas I should be aware of?

Cheers,

Tom
