# Multiple Routes

**URL:** <https://www.ruby-forum.com/t/multiple-routes/194712>\
**Category:** Rails\
**Created:** [September 9, 2010, 12:18pm UTC](https://www.ruby-forum.com/t/multiple-routes/194712 "2010-09-09T12:18:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Creative\_Technologi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@Creative\_Technologi](https://www.ruby-forum.com/u/Creative_Technologi)\
**Post date:** [September 9, 2010, 12:18pm UTC](https://www.ruby-forum.com/t/multiple-routes/194712/1 "2010-09-09T12:18:46Z")

</div>

Hi,

Could anyone help with this one.

I want to have two routes into my news stories for the show view, one  
by ID and the other by the URL of the story which is stored in the db.

I have two routes setup …

map.connect ‘itcontractormortgagenews/:id’, :controller =\>  
‘posts’, :action =\> ‘show’  
map.connect ‘itcontractormortgagenews/:url’, :controller =\>  
‘posts’, :action =\> ‘show’

and I have edited my show method in the controller to …

if params[:id].to\_i \> 0  
@post = Post.find(params[:id])  
else  
@post = Post.find\_by\_sql(“select \* from posts where URL = '” +  
params[:url] + “’”).first  
end

The mapping by URL works fine but by ID not. I’m sure there is a  
better way to do this.

Thanks

Richard.

---

<div class="post-metadata">

**Author:** ![Creative\_Technologi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@Creative\_Technologi](https://www.ruby-forum.com/u/Creative_Technologi)\
**Post date:** [September 9, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/multiple-routes/194712/2 "2010-09-09T14:53:21Z")

</div>

use the requirement attribute in the routes and use friendly\_id gem,

> **[\#70 Custom Routes - RailsCasts](http://railscasts.com/episodes/70-custom-routes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

this shows how the use requirement in routes , remember to put specific  
routes on top and more generic routes below

with frienly\_id gem you just have to config.gem and add

has\_friendly\_id :url in your model , the gem will take care of  
finding  
the record and will put the given field in the url query string

On Thu, Sep 9, 2010 at 6:14 AM, Creative Technologist \<

---

<div class="post-metadata">

**Author:** ![Creative\_Technologi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@Creative\_Technologi](https://www.ruby-forum.com/u/Creative_Technologi)\
**Post date:** [September 9, 2010, 11:40pm UTC](https://www.ruby-forum.com/t/multiple-routes/194712/3 "2010-09-09T23:40:23Z")

</div>

Perfect, good video link, thanks radhames!
