Has_many :through, restful routes design when creating on the join table

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

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
    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.

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).

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs