0

I have been working on a web app using a Rails API and AngularJS. As models I have Teams and Users. You can add existing users to your team at anytime.

Since I want to add users to a team from AngularJS I wonder which route / controller should handle this action. (Adding an existing users to team.users)

Would this be a PUT api/teams/:id/users or PUT api/teams/:id or even a POST to api/teams/:id/:users

Keep in mind I am not creating a new user here but appending an existing one to a team's users.

Thank you!

XuoriG
  • 600
  • 4
  • 17

1 Answers1

2

None of the above; I would POST to /memberships.

You're not creating or updating a user, and you're not creating or updating a team.

You're creating a new joining record for users and teams. I would call that type of record a Membership, and provide an API for managing them. When a user joins a team, it's a POST to /memberships. When a user leaves a team, it's a DELETE to /memberships/:id.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Makes a lot of sense. Would that require me to remove the has_and_belongs_to_many relationships I had and replace them with has_many through: ? – XuoriG Feb 03 '15 at 15:50
  • Yes, you shouldn't use `has_and_belongs_to_many` any ways. It's more or less out of favour at this point. – user229044 Feb 03 '15 at 16:35