RESTful way to merge resources

2019-06-11 00:39发布

问题:

Given two resources

  • http://example.com/places/1
  • http://example.com/places/2

How should the endpoint look like to merge both resources into a single resource? Ideally after the merge only 1 resource will remain.

At first glance I could do a PUT to http://example.com/places/1 with params[:src_id] = 2. Which would merge the attributes from the second resource into the first and delete the the second resource. Does anyone else have a better approach?

Note that I'm using Rails which doesn't support the http MERGE verb so that's out of the question.

回答1:

I suggest a new controller PlacesMergeController with a new and create action. In the new action, you would have a form asking which places to merge. The form will do a POST request to the create action, where you do the actual merging. Depending on the complexity of the logic you could also have a PlacesMerge model (non-AR!).

This approach should give you maximum flexibility in terms of handling source/target of the merge operation - and it's easily maintainable.