Single step batch delete in Ruby on Rails

Paul picture Paul · May 15, 2012 · Viewed 12.4k times · Source

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
);

Answer

jimworm picture jimworm · May 15, 2012

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