Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for Bulk Update in Controller

Tags:

I am wondering what is the best practice for allowing bulk edit / update in the controller. I really can't find articles or guides on the subject.

like image 394
Chris Ledet Avatar asked Nov 01 '11 11:11

Chris Ledet


1 Answers

I see that you tagged your question with REST.

To do it RESTfully, you need to think about the collection or the update itself as the resource.

Say you are working with Product objects.

You might PUT to /product_batches/[some identifier], which would call ProductBatchesController#update, but then you are stuck wondering what goes in [some identifier]. You could make ProductBatch a singular resource and then you wouldn't need an id.

Better might be to POST to /product_bulk_updates, which would call ProductBulkUpdatesController#create

class ProductBulkUpdatesController < ApplicationController    def create     # your magic here     # - update_all if you are making the same change to all Products     # - looping through the hashes in params[products] if you are passing in distinct changes to each.   end  end 

Here's another thread: Bulk Collection Manipulation through a REST (RESTful) API

like image 199
Robert Head Avatar answered Oct 26 '22 16:10

Robert Head