Use update_all instead of save to select and update particular columns

Use update_all instead of save to select and update particular columns

9ec6ef85b64fa6be8cdbe0c7367a5a2706ac6596

diff --git a/app/models/upload.rb b/app/models/upload.rb
index 76003d1..9f5e2f6 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -294,14 +294,14 @@ class Upload < ActiveRecord::Base
             remap_scope ||= begin
               Post.with_deleted
                 .where("raw ~ '/uploads/#{db}/\\d+/' OR raw ~ '/uploads/#{db}/original/(\\d|[a-z])/'")
-                .select(:raw, :cooked)
+                .select(:id, :raw, :cooked)
                 .all
             end
 
             remap_scope.each do |post|
               post.raw.gsub!(previous_url, upload.url)
               post.cooked.gsub!(previous_url, upload.url)
-              post.save!(validate: false) if post.changed?
+              Post.with_deleted.where(id: post.id).update_all(raw: post.raw, cooked: post.cooked) if post.changed?
             end
 
             upload.optimized_images.find_each(&:destroy!)

GitHub sha: 84cfe30c

What was the error being fixed here? Also, is there a reason we can’t do post.update_all(raw: post.raw, cooked: post.cooked)? This fix adds an extra query. Sorry my bad, it still does the update with one query.

Job exception: missing attribute: user_id - it is fixing this exception. update_all is not an instance method. We can use update_attributes! but under the hood it also a save! method.

1 Like

Ah icic. Good catch :+1:

2 Likes