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

Model class helper for ignoring prosopite on a specific query at runtime #56

Closed technicalpickles closed 12 months ago

technicalpickles commented 1 year ago

This is an extraction of some code we use with prosopite internally. There are a few places where we wanted to allow N+1, especially when it is outside of the normal web requests.

To do this, we add a class method to ActiveRecord::Base to add an annotation, and then we check it while checking against ignored queries.

Maybe not strictly necessary for this, but I also started a Railtie for prosopite for any rails configuration to be registered. That is where ActiveRecord::Base adds our module.

I also added zeitwerk for autoloading.

cc @geshwho who implemented this initially

charkost commented 1 year ago

I understand the need to ignore specific N+1 queries at runtime. However, isn't the existing Prosopite.pause enough for that job?

technicalpickles commented 1 year ago

Strictly speaking, you can get the same effect, but the interface would be different:

Leg.last(10).each do |l|
   Prosopite.pause { l.chair }
end

It feels natural to be able to chain it on a query like you do other scopes and helpers. It might be possible to have a class method that wraps the actual execution of the relationship to call Prosopite.pause but I'm not sure how to implement that yet.