From 596096b8d57f9dbe8768f5145a3c719bf481f4b8 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Sat, 4 May 2019 14:51:22 -0500 Subject: [PATCH] Prefer find_by over detect Uses a better SQL query to pull a single object vs. pulling a full set and then iterating over it. --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index df05ccbe..99a54528 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -28,7 +28,7 @@ class User < ApplicationRecord end def update_positions(project_ids) project_ids.each_with_index {|id, position| - project = self.detect { |p| p.id == id.to_i } + project = self.find_by(id: id.to_i) raise I18n.t('models.user.error_project_not_associated', :project => id, :user => @user.id) if project.nil? project.update_attribute(:position, position + 1) }