Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More efficient way of doing destroy_all in Rails?

Is there a quicker way of doing destroy all in rails?

I'm asking this because rails does an individual delete sql query per record.

For example calling destroy all on a search for 4 records would then run 4 delete sql commands.

Blerg.where("created_at > yesterday").destroy_all (4 results)

then calles…

DELETE FROM "blergs" WHERE "blergs"."id" = $1  [["id", 197782]]

4 times.

Is there a way to get it into one sql command?

like image 436
MintDeparture Avatar asked Nov 27 '12 16:11

MintDeparture


1 Answers

You can allways call

Blerg.where("created_at > yesterday").delete_all

This gives only one query:

DELETE FROM "blergs" WHERE (created_at > '2012-11-26 16:27:56.678872')

Note that this does not call any callbacks on the objects which are to be deleted.

like image 116
Carl Ekerot Avatar answered Oct 25 '22 17:10

Carl Ekerot