9

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
);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Paul
  • 25,812
  • 38
  • 124
  • 247

2 Answers2

9

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

jimworm
  • 2,731
  • 18
  • 26
0

For me, I use small query to iterate over deletes

while Topic.limit(1000).delete_all > 0 do
  next
end
Cc Won
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 06:35