I have several scenarios where I need to do some transformations on the params of my POST request. This POST API is part of an API only application. These are the several use-case or scenarios where I need the transformation:
- Currently, the API does not make use of accepts_nested_attributes_for. API accepts a POST request with a nested structure in the body and explicitly calls save! on the parent object and then on the child object inside a transaction. I would like to make use of accepts_nested_attributes_for and hence I need to accept
child_attributeskey inside my params. Currently, I just have a key for
children. Thus, I need to rename a key in the param. Hence, my questions are:
- Is it worth using the accepts_nested_attributes for?
- Where do I keep the param transformation code? Before the strong parameters permit or after?
- There are other scenarios where I deduce several keys based on a single key from the params. For eg. the API request sends a place_id and we make a call to the Google Places API to retrieve additional data based on the place_id and store it in the database. Thus, based on a place_id, we also store the location_name in the model. Where should such code reside which is populating location_name based on place_id?
- Before the permit method in the controller?
- After the permit method in the controller?
- Inside the model by overriding the attribute setter for place_id?
- Any other place?
Note that I also need to throw a 422 if the place_id is invalid.
Also, I cannot change the structure of the body in the incoming POST request since it is already used by a lot of applications.