eephyne eephyne - 3 months ago 11
reST (reStructuredText) Question

RESTful way in rails to edit multiple DB entries at the same time

To configure the admin settings of my app I made a Admin controller and a AdminConfig model.

The Admin controller have several categories (system,files,folders,…) and I wanted to easily separate them in the AdminConfig model by setting a category column in the db.
But I really want to do it in a RESTful way and I don't know how to do it with rails.

Rails want to edit each db row separately, but I want to group them.

example: on the files category I have:


  • extension to look for

  • exif tags to ignore

  • subfolder depth



each one is a separate entry in the AdminConfig model like that:


  • name | value| category

  • extension | .jpg,.png,.avi | files

  • exif_tag | nil | files

  • subfolder_depth | 3 | files



(sorry table not working here)

So I want to show all of them on the page and when I edit them I want to update them all. But I don't know how to do it properly.

I can do it my way obviously or modify the DB so one row == one category but thats not how I want to achieve it because it'll be more difficult to add more settings after.

Answer

If all of that settings belong to a same collection (e.g. several ExifTag models) – you can patch a collection, that's not prohibited in REST (though you'll have to define your own action, there's no predefined one for patching a collection in Rails).

PATCH [{id: 3, name: 'Paul'}, {id: 4, name: 'Dan'}]

If all that settings belong to one model – you can patch that model and pass data to other models as nested attributes, Rails allows you to do that.

PATCH {id: 1, hobbies_attributes: [{id: 1, name: 'Hiking'}], house_attributes: { location_attributes: { city: 'Vancouver' }}

If non of these, like you want to patch 2 different models unrelated to each other - then there's no "healthy" way to do that in REST. I can suggest to go with some fancy frontend, which will send different requests to different routes upon editing, updating different models; That would be the cleanest way.

Comments