FIX: Use find_by_id method to prevent record not found exception

FIX: Use find_by_id method to prevent record not found exception
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb
index 9c9ed22..910f59e 100644
--- a/lib/cooked_post_processor.rb
+++ b/lib/cooked_post_processor.rb
@@ -513,7 +513,7 @@ class CookedPostProcessor
       end
 
       upload_id = downloaded_images[src]
-      upload = Upload.find(upload_id) if upload_id
+      upload = Upload.find_by_id(upload_id) if upload_id
       img["src"] = upload.url if upload.present?
 
       # make sure we grab dimensions for oneboxed images
diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb
index 829a4e8..5a783cf 100644
--- a/spec/components/cooked_post_processor_spec.rb
+++ b/spec/components/cooked_post_processor_spec.rb
@@ -778,6 +778,12 @@ describe CookedPostProcessor do
       cpp.post_process_oneboxes
 
       expect(cpp.doc.to_s).to eq("<p><img class=\"onebox\" src=\"#{upload.url}\" width=\"\" height=\"\"></p>")
+
+      upload.destroy
+      cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
+      cpp.post_process_oneboxes
+
+      expect(cpp.doc.to_s).to eq("<p><img class=\"onebox\" src=\"#{image_url}\" width=\"\" height=\"\"></p>")
     end
 
     it "replaces large image placeholder" do

GitHub
sha: 0d3c1cde

Aren’t find_by_<something>(xxx) deprecated in favor of find_by(something: xxx)?

1 Like

All dynamic methods except for find_by_… and find_by_…! are deprecated.

Ruby on Rails 4.0 Release Notes — Ruby on Rails Guides

No

2 Likes

If we’re not checking for the return value, I think destroy! over destroy is better.

3 Likes

Done DEV: Use destroy! method to raise error if any (#0d3c1cde) · discourse/discourse@efcea14 · GitHub :+1:

2 Likes