# REST: Do I have to use the primary\_key in the path?

**URL:** <https://www.ruby-forum.com/t/rest-do-i-have-to-use-the-primary-key-in-the-path/121328>\
**Category:** Rails\
**Created:** [November 26, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/rest-do-i-have-to-use-the-primary-key-in-the-path/121328 "2007-11-26T22:12:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 26, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/rest-do-i-have-to-use-the-primary-key-in-the-path/121328/1 "2007-11-26T22:12:38Z")

</div>

Is there a way to use something other than the an object’s ID in a  
REST-ful API?

For example, if an application stores a collection of plants, can a  
plant be referenced by the ‘scientific\_name’ rather than the  
‘primary\_key’? If so how?

Taking it one step further, is there a way to automatically strip out  
underscores from the scientific name before doing a find?

This would be an ideal URL:

/plants/Lilium\_humboldtii (instead of /plants/345)

where this plant’s scientific name is ‘Lilium humboldtii’.

Thanks,  
Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 27, 2007, 1:12am UTC](https://www.ruby-forum.com/t/rest-do-i-have-to-use-the-primary-key-in-the-path/121328/2 "2007-11-27T01:12:58Z")

</div>

On Nov 26, 2007 4:11 PM, Scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This would be an ideal URL:
> 
> /plants/Lilium\_humboldtii (instead of /plants/345)
> 
> where this plant’s scientific name is ‘Lilium humboldtii’.

You can achieve this in a number of different ways. I like the way the  
friendly\_identifier plugin works.

[http://agilewebdevelopment.com/plugins/friendly\_identifier](http://agilewebdevelopment.com/plugins/friendly_identifier)

Michael G.

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 27, 2007, 5:33am UTC](https://www.ruby-forum.com/t/rest-do-i-have-to-use-the-primary-key-in-the-path/121328/3 "2007-11-27T05:33:40Z")

</div>

Scott wrote:

> Is there a way to use something other than the an object’s ID in a  
> REST-ful API?
> 
> For example, if an application stores a collection of plants, can a  
> plant be referenced by the ‘scientific\_name’ rather than the  
> ‘primary\_key’? If so how?
> 
> Taking it one step further, is there a way to automatically strip out  
> underscores from the scientific name before doing a find?
> 
> This would be an ideal URL:
> 
> /plants/Lilium\_humboldtii (instead of /plants/345)
> 
> where this plant’s scientific name is ‘Lilium humboldtii’.

You can use anything in a route (subject to HTTP URL format specs). In  
your model, you can override #to\_param to change what gets put in the  
URL by the \_path helper, and in the controller you can use params[:id]  
any way you’d like. To work with a user URL like so:

User:

def to\_param  
self.username  
end

UsersController:

def show  
@user = User.find\_by\_username(params[:id])  
end

That will work with the standard map.resources routes (as long as the  
param doesn’t contain route separator chars (period, slash, question  
mark).

Swapping spaces and underscores is easy. Just use #gsub.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
