From ccee193f388a0886f10f46265bc8a085e20a426b Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Sun, 31 Dec 2023 02:02:31 +0000 Subject: [PATCH] Cache the result of `Journal#attachments` (#39847). Patch by Go MAEDA (@maeda). git-svn-id: https://svn.redmine.org/redmine/trunk@22581 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/journal.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/journal.rb b/app/models/journal.rb index cc5611754..c19a63816 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -142,8 +142,10 @@ class Journal < ActiveRecord::Base end def attachments - ids = details.select {|d| d.property == 'attachment' && d.value.present?}.map(&:prop_key) - Attachment.where(id: ids).sort_by {|a| ids.index(a.id.to_s)} + @attachments ||= begin + ids = details.select {|d| d.property == 'attachment' && d.value.present?}.map(&:prop_key) + ids.empty? ? [] : Attachment.where(id: ids).sort_by {|a| ids.index(a.id.to_s)} + end end def visible?(*args)