# Has\_many :through, restful routes design when creating on the join table

**URL:** <https://www.ruby-forum.com/t/has-many-through-restful-routes-design-when-creating-on-the-join-table/132318>\
**Category:** Rails\
**Created:** [March 19, 2008, 4:45am UTC](https://www.ruby-forum.com/t/has-many-through-restful-routes-design-when-creating-on-the-join-table/132318 "2008-03-19T04:45:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@dusty](https://www.ruby-forum.com/u/dusty)\
**Post date:** [March 19, 2008, 4:45am UTC](https://www.ruby-forum.com/t/has-many-through-restful-routes-design-when-creating-on-the-join-table/132318/1 "2008-03-19T04:45:04Z")

</div>

Here is a design related question to those out there that are using  
restful routing with rails. What I’m trying to figure out a good way  
to design my URLs for creating objects on the join table.

For example, take the classic example of friendships. Say a user has  
many friends, through friendships. A user also has many fans, through  
friendships, by switching the :source flag. How would you create that  
friendship?

Assume I have friends and fans as a nested resource of users for this  
example. I have access to self.current\_user, so I can easily check to  
make sure one is posting to his own resource.

1. Define the URL with a query parameter  
POST /users//friends?friend\_id=  
DELETE /users//friends/

2. Keep the URL clean, by posting to the friends fans controller  
instead  
POST /users/\<friend\_id\>/fans  
DELETE /users//friends/\<friend\_id\>

3. Edit the route to add a friend\_id to the create method in the  
friends controller  
POST /users//friends/\<friend\_id\>  
DELETE /users//friends/\<friend\_id\>

There are plenty of more ways to do this, and all of them are easy to  
do. What do most people use? I haven’t found too many topics out  
there about how people are creating the join resource. Is it common  
to do things like POST to one resource and DELETE from another? Or,  
to POST to a resource but add a query param to the path to get all the  
required info?

Thanks for any tips or references you wish to share.

---

<div class="post-metadata">

**Author:** ![dusty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@dusty](https://www.ruby-forum.com/u/dusty)\
**Post date:** [March 19, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/has-many-through-restful-routes-design-when-creating-on-the-join-table/132318/2 "2008-03-19T14:21:09Z")

</div>

My inclination would be to go with #2 though I’d do all the ‘fan’  
things through the fans controller. By taking this approach you  
decouple the public interface from its implementation details,  
allowing you to change your underlying domain if you find a need to do  
so in the future (e.g., you decide that ‘fandom’ is sufficiently from  
‘friendship’ that you want to carry different info in the join table).
