charkost / prosopite

:mag: Rails N+1 queries auto-detection with zero false positives / false negatives
Apache License 2.0
1.53k stars 46 forks source link

Handling Bulk `find_or_create` Operations? #36

Closed choxi closed 2 years ago

choxi commented 2 years ago

We use a pattern like this often in our codebase:

user_attributes.each do |user_attrs|
  user = User.find_or_create_by!(email: user_attrs[:email], account: user_attrs[:account]) do |u|
    u.name = user_attrs[:name]
  end
end

This triggers a Prosopite error because of the sequential SELECTs. What's a good way to have bulk operations like this ignored by Prosopite?

charkost commented 2 years ago

You can use any of the options described in the 'Allow list' section of the README or the pause/resume api:

Prosopite.pause
user_attributes.each do |user_attrs|
  user = User.find_or_create_by!(email: user_attrs[:email], account: user_attrs[:account]) do |u|
    u.name = user_attrs[:name]
  end
end
Prosopite.resume