dusty
2008-Mar-19 03:44 UTC
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 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/<myself>/friends?friend_id=<mynewfriend> DELETE /users/<myself>/friends/<myoldfriend> 2. Keep the URL clean, by posting to the friends fans controller instead POST /users/<friend_id>/fans DELETE /users/<myself>/friends/<friend_id> 3. Edit the route to add a friend_id to the create method in the friends controller POST /users/<myself>/friends/<friend_id> DELETE /users/<myself>/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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AndyV
2008-Mar-19 13:20 UTC
Re: has_many :through, restful routes design when creating on the join table
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). On Mar 18, 11:44 pm, dusty <dusty.do...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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/<myself>/friends?friend_id=<mynewfriend> > DELETE /users/<myself>/friends/<myoldfriend> > > 2. Keep the URL clean, by posting to the friends fans controller > instead > POST /users/<friend_id>/fans > DELETE /users/<myself>/friends/<friend_id> > > 3. Edit the route to add a friend_id to the create method in the > friends controller > POST /users/<myself>/friends/<friend_id> > DELETE /users/<myself>/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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---