Skip to content

Commit

Permalink
Merge pull request #3875 from BenMorganIO/improve-update-positions-pe…
Browse files Browse the repository at this point in the history
…rformance

eager load records instead of n+1 for update_positions
  • Loading branch information
kennyadsl authored Apr 30, 2021
2 parents d0e27f5 + cab33db commit 248b39f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ def create

def update_positions
ActiveRecord::Base.transaction do
params[:positions].each do |id, index|
model_class.find_by(id: id)&.set_list_position(index)
positions = params[:positions]
records = model_class.where(id: positions.keys).to_a

positions.each do |id, index|
records.find { |r| r.id == id.to_i }&.set_list_position(index)
end
end

Expand Down

0 comments on commit 248b39f

Please sign in to comment.