Skip to content

Commit

Permalink
ガントチャート画面表示時のN+1問題解消
Browse files Browse the repository at this point in the history
  • Loading branch information
kumojima committed Feb 9, 2024
1 parent 990b81c commit a2eedaa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/redmine/helpers/gantt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def selected_column_content(options={})
def issues
@issues ||= @query.issues(
:order => ["#{Project.table_name}.lft ASC", "#{Issue.table_name}.id ASC"],
:limit => @max_rows
:limit => @max_rows,
:include => [:children],
)
end

Expand Down
11 changes: 2 additions & 9 deletions lib/redmine/nested_set/traversing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def self.included(base)
base.class_eval do
scope :roots, lambda {where :parent_id => nil}
scope :leaves, lambda {where "#{table_name}.rgt - #{table_name}.lft = ?", 1}

has_many :children, -> { order(:lft) }, class_name: self.class_name, foreign_key: 'parent_id'
end
end

Expand All @@ -47,15 +49,6 @@ def root
self_and_ancestors.first
end

# Returns the children
def children
if id.nil?
nested_set_scope.none
else
self.class.order(:lft).where(:parent_id => id)
end
end

# Returns the descendants that have no children
def leaves
descendants.where("#{self.class.table_name}.rgt - #{self.class.table_name}.lft = ?", 1)
Expand Down

0 comments on commit a2eedaa

Please sign in to comment.