Patch #26711
closed
Use pluck instead of collect/map
Added by jwjw yy about 7 years ago.
Updated about 6 years ago.
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
- Category set to Code cleanup/refactoring
- Target version set to 4.1.0
- Tracker changed from Defect to Patch
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).
- Subject changed from .map should be changed to .pluck to Use pluck instead of map in MyHelper
- Status changed from New to Closed
- Assignee set to Go MAEDA
- Target version changed from 4.1.0 to 4.0.0
Commited in the trunk. Thank you for pointing it out.
- Subject changed from Use pluck instead of map in MyHelper to Use pluck instead of collect/map
- Category changed from Code cleanup/refactoring to Performance
- Related to Patch #26726: Use pluck(:id) instead of collect(&:id) added
Also available in: Atom
PDF