Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single step batch delete in Ruby on Rails

How to send such a query to database server from Rake task without removing record-by-record in "each" loop?

delete from data
where uuid in (
    select uuid
    from data
    group by uuid, raw, recdate
    having count(*)>1
);
like image 564
Paul Avatar asked May 15 '12 10:05

Paul


1 Answers

ActiveRecord has the delete_all method for you. Note that it does not call the destroy callbacks. http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all

like image 118
jimworm Avatar answered Oct 10 '22 06:10

jimworm