# Multiple ids on restful action

**URL:** <https://www.ruby-forum.com/t/multiple-ids-on-restful-action/126711>\
**Category:** Rails\
**Created:** [January 21, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/multiple-ids-on-restful-action/126711 "2008-01-21T16:18:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 21, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/multiple-ids-on-restful-action/126711/1 "2008-01-21T16:18:21Z")

</div>

Hi,

I know this question has come up before, but I couldn’t find a  
satisfying answer.  
Maybe I didn’t look well enough, so any pointers to old messages  
regarding this subject would be welcome too.

I need some way to pass multiple ids to a resource.  
Something like /posts/13,14  
If I do this I have to manually split the id on ","s in the action,  
and use  
post\_path([post1, post2].map {|x| x.id}.join(’,’))  
in my views, which I think is not very nice.

If I use post\_path([post1, post2]) rails uses to\_param on the array  
which leads to the string “13/14” which is url-encoded to “13%2F14”  
but the browser just shows a / in the next screen.  
This is nice, since now the controller can split on “/” and I don’t  
need to do any string-manipulation in my views.  
Only, if a user wants to enter the url by himself (/posts/13/14) I  
will get a routing error because now the / isn’t url-encoded. I can’t  
expect users to type %2F instead, which just looks ugly.  
Now I could override Array’s to\_param method, but I’m sure there is a  
reason for the default behavior.

So what is the preferred way to solve this? any transparent way to  
handle this?  
I mean ActiveRecord handles Post.find(13,14) just fine,  
Post.find([13,14]) also.  
Is there a way to make ActionController’s routing automatically give  
params[:id] back as an array if it detects some pattern? (something  
like from\_param maybe?)

Thanks for any help.  
Mathijs

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 21, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/multiple-ids-on-restful-action/126711/2 "2008-01-21T16:41:03Z")

</div>

you could use \*ids in your routing which would allow for an array of ids  
using a non encoded /

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 21, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/multiple-ids-on-restful-action/126711/3 "2008-01-21T23:51:22Z")

</div>

Cool,

And how can I do this on RESTful routes?  
map.resources :posts …

Will it work too if I use nesting?  
/posts/13/comments

Greetings,  
Mathijs

On 21 jan, 16:41, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
