Topic: RESTful friends system

I used to have actions like add_friend, remove_friend, and show_friends on my users controller but I'm trying to redo it in a RESTful way, so I'm just wondering how the database and new model/controller should be set up.

My old database had a table called friends_users that had user_id and friend_id columns. In the RESTful way I'm thinking of naming the relationship between users "friendships" and then having a new model/controller for that. Should I now have a table called friendships with id, user_id and friend_id columns? Or should I just leave the table as is? It seems like friendships need their own id to be able to have routes like /friendships/1. That route looks weird to me though, because its identifying the friendship instead of the user(s).

Also, how would you show all the friends for a particular user? With /friendships/index? Shouldn't there be a unique url for each user's friends page (like /users/1/friends)?

Should it even be called "friendships"? That seems weird to me too, but when DHH was explaining users and groups, he named the relationship "membership" to turn it into a RESTful design.

Re: RESTful friends system

fluxcapacitor wrote:

My old database had a table called friends_users that had user_id and friend_id columns. In the RESTful way I'm thinking of naming the relationship between users "friendships" and then having a new model/controller for that. Should I now have a table called friendships with id, user_id and friend_id columns? Or should I just leave the table as is? It seems like friendships need their own id to be able to have routes like /friendships/1. That route looks weird to me though, because its identifying the friendship instead of the user(s).

You are on the right track. Creating a Friendship model is the right way to go. The table would have an "id" column as the other model tables do.

fluxcapacitor wrote:

Also, how would you show all the friends for a particular user? With /friendships/index? Shouldn't there be a unique url for each user's friends page (like /users/1/friends)?

You would need to pass the user_id to the friendships controller: /friendships?user_id=1

Or if you go with nested routes it would look like this: users/1/friendships

However, it depends on the interface. You may not even need to list the friends on a separate page. You could list them on the user's show page along with other data about the user. you could just use the friendships controller for creating/adding friends to a user - it just replaces the add_friend and remove_friend actions in the users controller.

fluxcapacitor wrote:

Should it even be called "friendships"? That seems weird to me too, but when DHH was explaining users and groups, he named the relationship "membership" to turn it into a RESTful design.

Friendship is an excellent word for this.

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful friends system

Cool - thanks for the help Ryan. I think I'll probably just show the friends on the user's show page like you suggested.