Patch #26711

.map should be changed to .pluck

Added by jwjw yy about 1 month ago. Updated about 1 month ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Code cleanup/refactoring
Target version:4.1.0

Description

In app/helpers/my_helpers,
68: where(:project_id => User.current.projects.map(&:id)).
Use pluck to get the id is much more efficient, since the query will only access the id field, instead of select * from projects:
Before:
SELECT `projects`.* FROM `projects` INNER JOIN `members` ON `projects`.`id` = `members`.`project_id` WHERE `members`.`user_id` = ? AND (projects.status<>9) "user_id", 5341

After:
SELECT `projects`.`id` FROM `projects` INNER JOIN `members` ON `projects`.`id` = `members`.`project_id` WHERE `members`.`user_id` = ? AND (projects.status<>9) "user_id", 5341

History

#1 Updated by Toshi MARUYAMA about 1 month ago

  • Category set to Code cleanup/refactoring
  • Target version set to 4.1.0

#2 Updated by Toshi MARUYAMA about 1 month ago

  • Tracker changed from Defect to Patch

#3 Updated by Go MAEDA about 1 month ago

Index: app/helpers/my_helper.rb
===================================================================
--- app/helpers/my_helper.rb    (revision 16952)
+++ app/helpers/my_helper.rb    (working copy)
@@ -78,7 +78,7 @@
   def render_calendar_block(block, settings)
     calendar = Redmine::Helpers::Calendar.new(User.current.today, current_language, :week)
     calendar.events = Issue.visible.
-      where(:project_id => User.current.projects.map(&:id)).
+      where(:project_id => User.current.projects.pluck(:id)).
       where("(start_date>=? and start_date<=?) or (due_date>=? and due_date<=?)", calendar.startdt, calendar.enddt, calendar.startdt, calendar.enddt).
       includes(:project, :tracker, :priority, :assigned_to).
       references(:project, :tracker, :priority, :assigned_to).
@@ -139,7 +139,7 @@

   def render_news_block(block, settings)
     news = News.visible.
-      where(:project_id => User.current.projects.map(&:id)).
+      where(:project_id => User.current.projects.pluck(:id)).
       limit(10).
       includes(:project, :author).
       references(:project, :author).

Also available in: Atom PDF