Mvc – Do Rails Join Models Get Controllers

mvcruby-on-rails

I have a rails app where my users can buddy up with other users. Since that relationship can have a status (approved/rejected/pending), I decided to go with a join model (UserRelationship) so now I have the attribute.

To handle state for that join model, I have a controller (UserRelationshipsController) with actions/RESTful endpoints for changing the state of the relationship. Is this bad practice? If so, where should I handle state-changes for the relationship? In the User model?

Best Answer

You can treat UserRelationship as a separate resource (as you proposed in your question) or you can use Nested resources (http://guides.rubyonrails.org/routing.html#nested-resources) to have something like this

POST user/9/relationship (POST/PATCH: user_id: 10, status: 'confirmed')

to set/get relationship statuses for specific user.

Related Topic