From 754458e6a277752557a56b7f3988ebb82034f01c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 28 May 2018 16:41:38 +0800 Subject: [PATCH] PERF: Minor but we can just pass a hash to `where`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` Warming up -------------------------------------- 4 x .where 1.040k i/100ms 1 x .where 1.677k i/100ms Calculating ------------------------------------- 4 x .where 10.321k (± 5.2%) i/s - 52.000k in 5.053803s 1 x .where 17.117k (± 3.8%) i/s - 85.527k in 5.004107s Comparison: 1 x .where: 17117.1 i/s 4 x .where: 10321.3 i/s - 1.66x slower ``` --- app/models/notification.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index c476f778b38..8e213851bc7 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -59,18 +59,22 @@ class Notification < ActiveRecord::Base def self.mark_posts_read(user, topic_id, post_numbers) Notification - .where(user_id: user.id) - .where(topic_id: topic_id) - .where(post_number: post_numbers) - .where(read: false) + .where( + user_id: user.id, + topic_id: topic_id, + post_number: post_numbers, + read: false + ) .update_all(read: true) end def self.read(user, notification_ids) Notification - .where(id: notification_ids) - .where(user_id: user.id) - .where(read: false) + .where( + id: notification_ids, + user_id: user.id, + read: false + ) .update_all(read: true) end