Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the restful way to delete multiple items in rails?

How to delete multiple records in rails by passing the multiple values to REST api?. My current routes looks like this:

products DELETE /products/:id(.:format)   products#destroy

When I try to pass multiple values as as an array (DELETE /products/[ids]), it says the routes doesn't exists.

like image 587
Srikanth Gurram Avatar asked Nov 19 '15 13:11

Srikanth Gurram


People also ask

What is REST API in Rails?

REST stands for REpresentational State Transfer and describes resources (in our case URLs) on which we can perform actions. CRUD , which stands for Create, Read, Update, Delete, are the actions that we perform. Although, in Rails, REST and CRUD are bestest buddies, the two can work fine on their own.


2 Answers

If you're doing that pass an array of ids and use destroy_all

Product.where(id: params[:ids]).destroy_all
like image 171
j-dexx Avatar answered Oct 03 '22 19:10

j-dexx


After a lot research, I have figured out a way to delete multiple records by passing the ids as a comma separated values to REST api.

You have call the API like this

DELETE /products/id1,id2,id3 

Now handle the business logic in your controller

Product.where(id: params[:id].split(',')).destroy_all
like image 24
Srikanth Gurram Avatar answered Oct 03 '22 18:10

Srikanth Gurram