From 789f2b48076b2917e2fbb0ea46f32cb9937c5272 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Sat, 16 Jun 2018 12:14:49 +0200 Subject: [PATCH] Refactored code layout and prefer pluck over select to avoid unnecessary memory allocation. --- app/models/application_model/can_latest_change.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/models/application_model/can_latest_change.rb b/app/models/application_model/can_latest_change.rb index caaa6456e..def47cb03 100644 --- a/app/models/application_model/can_latest_change.rb +++ b/app/models/application_model/can_latest_change.rb @@ -21,14 +21,13 @@ returns key = "#{new.class.name}_latest_change" updated_at = Cache.get(key) + return updated_at if updated_at + # if we do not have it cached, do lookup - if !updated_at - o = select(:updated_at).order(updated_at: :desc, id: :desc).limit(1).first - if o - updated_at = o.updated_at - latest_change_set(updated_at) - end - end + updated_at = order(updated_at: :desc, id: :desc).limit(1).pluck(:updated_at).first + + return if !updated_at + latest_change_set(updated_at) updated_at end